Lucid: Polymorphic Relations

Created on 13 Jan 2017  路  8Comments  路  Source: adonisjs/lucid

is there a way to handle this relations?

Most helpful comment

@thetutlage
I do not fully agree with your reason for "Why Polymorphic relations are bad".
Actually polymorphic relationships are used for some defined cases, to minimize the use of multiple tables for allocating same type of data.

Just for the sake of an example, there is a case as defined in Laravel Docs:

If there is a table structure like this:

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.

I think in the above example polymorphic relationship plays a major role in minimizimg tables & their structure.

All 8 comments

Nope, not right now. I will develop this feature soon. Keep the task open.

Is this will be available for 4.0?

I have personally never felt the use of Polymorphic relationships and they have so many issues in performance and general database design.

Now bad practices is a subjective term and I do not use it to justify my point, but Polymorphic is really something that should be avoided to have a cleaner database schema.

SQL databases has no issues with the number of table one can have, so it's always better to create a separate table that anyone can see and tell that what it does, over checking all the type columns to see that.

Also JOINS on varchar table is the worst thing you can do to SQL. So I have no plans to add it to the core.

Also doing a quick google search on Why polymorphic relations are bad will answer a lot of questions

Seems like this is being discussed again on Discord given the enniel/adonis-lucid-polymorphic package doesn't seem to be maintained/compatible with 4.x. Has there been a decision made to add polymorphic relationships to the core?

@rdunk I think @thetutlage was clear with the decision. I used to like polymorphic relationships but after meditating really well, I think We don't need it

@thetutlage
I do not fully agree with your reason for "Why Polymorphic relations are bad".
Actually polymorphic relationships are used for some defined cases, to minimize the use of multiple tables for allocating same type of data.

Just for the sake of an example, there is a case as defined in Laravel Docs:

If there is a table structure like this:

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

Two important columns to note are the commentable_id and commentable_type columns on the comments table. The commentable_id column will contain the ID value of the post or video, while the commentable_type column will contain the class name of the owning model. The commentable_type column is how the ORM determines which "type" of owning model to return when accessing the commentable relation.

I think in the above example polymorphic relationship plays a major role in minimizimg tables & their structure.

@thetutlage @saumya04
From my experience "Polymorphic Relations" are very powerfull ! I have not seen anything bad about them, at least so far and so far my projects and experience go. In fact sometimes, actually most of the time, not only they are more powerful than many to "many to many relationships", they are also easier to deal with. They are also far much more future-proof! Just plan or design your database (data structure) very well.

Was this page helpful?
0 / 5 - 0 ratings