Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are polymorphic relationships bad practice?

I'm using Laravel and have run into the need to have something just like polymorphic relationships. It would seem that they solve exactly what I need, which is being able to store different data based on a type, in a nutshell.

However, I'm aware that I would not be able to use any constraints, like foreign keys. And I've read around that they're essentially an anti-pattern. But I was wondering what others think about it, and if anyone is using them without issues.

Otherwise, is there an alternative way I could accomplish what polymorphic relationships provide, without actually using them? (Using Laravel)

like image 888
kenshin9 Avatar asked Sep 30 '15 05:09

kenshin9


1 Answers

Polymorphic relations are an anti-pattern and breaks best practices principles as per mjolnic comment.

Having said this, do you never store json in BLOB or TEXT column? Does Wordpress not store data serialized in mysql?

In the real-world, (outside of university), there are many scenarios where it is a good idea to break the rules. Depending on your specific use-case, it may be appropriate to break them.

simple blog example. You have posts, you have pages. They both need exactly the same table format. The benefits gained from Polymorphic relations far outweigh the effort put into normalising and also development and maintenance when splitting the content into posts and pages.

like image 63
Gravy Avatar answered Sep 30 '22 06:09

Gravy