Question

Model User has many Project has many Task

User(id, email, password)
Project(id, user_id, name)
Task(id, user_id, project_id, title)

Is it a good practice to have a cache column of higher parents like user_id on the Task model for faster access without making joins, or i should just stick to the good ol :through solution?

Would love to hear thoughts on this because i've always been worried if this does more harm than good.

Was it helpful?

Solution

I wouldn't say it's good practice to do that. You now have to keep that additional key in sync with the same key on the parent, and the data is not as normalised as it could be.

I wouldn't go down that path unless you have a reason to do so.

If the reason is you're finding it too inconvenient to have to keep going through the model hierarchy to get to the related User models, then perhaps there is a better way you can express that code so you don't have to keep doing so many lookups / joins?

If it's mainly for performance reasons, there might be better solutions though - caching, database indexes, and other techniques that may help.

If adding the extra key is the best/easiest solution for what you're trying to do, then fine. I would just consider whether there is a better way first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top