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)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With