lundi 5 juillet 2021

How to reuse a model to relate with multiple models

I've worked on big projects before, but I'm trying to improve my best practices, and one thing that I'm stuck on is not to create many models.

This might seem a little bit confusing, so let me put an example:

Let's suppose I have a Post model, and an Answer model, the answer one relates to the Post in a One-Many relationship.

Then, I want to add a Comment model, both to Post and Answer.

I could add two Foreign Key nullable columns on the Comment, to show which model it belongs.

But I could also create PostComment and AnswerComment models, removing the nullable column, but creating more kind of boilerplate.

Which practice is the best?

Aucun commentaire:

Enregistrer un commentaire