0

I am trying to create a pivot table to help keep track of "challenges" in my applications. Basically I have a challenge_task pivot table that creates a relationship between a challenge and a task. When a user that is in a challenge completes a task I want to be able to tell so I can track a user's progress. How is the best way to store multiple users completing a task on a challenge?

I was thinking in the pivot table adding a json column called user_completed to handle this and store the user_id for every user that completes the task for a challenge.

So challenge_task would look like

challenge_id | task_id | user_completed

Is this a good way? Or is there anything that fits this better?

9
  • 3
    "multiple values in one db column" is almost always a bad idea. Commented Sep 25, 2017 at 19:17
  • @Uueerdo if a challenge only will have two users then should I do like a user_a_completed and user_b_completed column on the pivot table and add the user_id for when a user completes them Commented Sep 25, 2017 at 19:19
  • 4
    Just make a new row each time. No need to store multiple user id's in one row. Commented Sep 25, 2017 at 19:19
  • You can use json columns in MySQL past a certain version (don't remember the number) but from my experience, this was more of a hinderance than a help. Querying json columns is a bit messy and not really supported yet by Laravel, any benefits added by them were quickly replaced by other issues, etc. The eloquent model/relationship system also works wonders for decreasing complexity when querying/return results with one-to-many or many-to-many relationships. In the end, up to you. Commented Sep 25, 2017 at 19:20
  • As #Uueerdo and #Brian Glaz said Commented Sep 25, 2017 at 19:20

3 Answers 3

1

I'd recommend a database structure something like this:

challenge: challenge_id | other data
task: task_id | other data
user: user_id | other  data

challenge_task: challenge_task_id | challenge_id | task_id 
               | possibly more data (such as deadline for completion)

challenge_task_users: challenge_task_id | user_id
                     | possibly more data (such as status: accepted, in progress, completed)
Sign up to request clarification or add additional context in comments.

Comments

0

I dont recommend Json if you want to index your data, because Json can not be indexed. I think you should make a pivot table between the users and the tasks too, and create the neccesary relations.

Comments

0

I wouldn't recommend you inserting multiple values in one database column.


Note: This is my opinion. Just sharing the way I use it.

A table called tasks_settings which has the task settings.

Example 1

I find this way flexible because I can always edit the title, description, and reward easily. I can also add 2 more fields here which are valid_till and valid_for. So you can make it expire after a period of time and only for a special rank like staff or all users.

Another table called users_tasks

Example 2

This one controls the users. Whether they have completed the task or not. This could also achieve what you are looking for.

id | challenge_id | task_id | username | user_completed

I hope this has helped you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.