How can you represent inheritance in a database? (2010)
stackoverflow.com
stackoverflow.com
If you only skimmed the StackOverflow post or were about to bounce because “my models don’t form complex inheritance hierarchies,” ask whether you’ve tried to serialize something with type `A | B` to the database and been frustrated at there being no good solution. Then (re-)read the post in that framing and see if the solutions proposed and tradeoffs discussed look more relevant to you.
It’s made me realize that there’s probably a lot more collected wisdom locked up in writings from the 90’s and early 00’s that I disregard because it’s so heavily class inheritance focused. Alternatively: free blog post ideas by re-contextualizing this content in contemporary languages and technologies!
> realize that there’s probably a lot more collected wisdom locked up in writings from the 90’s and early 00’s
Yes and no, it's the "design patterns" discussion all over again. Some of it was genuinely interesting, but most of it just was incidental complexity caused by early Java and the absolute horror of pre-standard C++. When you filter that out so that only timeless foundational concepts remain, you'll find that most of it had already been said before.
Not my favorite job. Learn to do a proper database design; it's not that hard. Balance denormalization against querying needs (disk space is cheap). Use json serialized objects to your advantage for complex things that you don't actually query on. The price you pay for adding complexity to a database is query time overhead, joins, and the resulting application level complexity, bugs, and overhead. You'll need lots of columns with indices to support all that. The queries get more complex and expensive. Etc. Needless complexity at the database level is not a good thing. Keep it simple.
Mostly, you don't need your database model to resemble your domain model. You just need it to store it. A simple database model could be a table with a key and a json blob. Perfectly valid for a lot of domain models. And databases like postgres support creating indices on things inside your json. So, you don't even lose the ability to query. If you really need extra columns (e.g. a timestamp), add them of course.
The mistake that people make is assuming that all the little objects in their domain are equally important. They are not. Most of it is just meta data that needs to be attached to something that is never/rarely used for querying. It needs to live somewhere. But that doesn't necessarily have to be a dedicated column or table in a database.
I understand why strict moderation standards are needed on a site like SO, but IMO that site started huffing too much of its own supply long ago.
"You have an online shop with products that have some common attributes but each category of products also has it's specific attributes.
How would you model this in your code and database?"
The question is a bit more interesting if the database is relational.
Lots of answers exist, all with some trade-offs.
https://docs.jboss.org/hibernate/orm/6.6/introduction/html_s...
Technically most people I have seen just use a variant of 3, with @MappedSuperclass.
The question and answers are from 2010 and the PostgreSQL JSON data type was introduced in 2012, so it makes sense that this rather awful solution hadn't caught on yet. The "NoSQL" movement was just gathering steam in 2010.
type ConnectEvent extending RTCEvent, SoftDeletable { handle: str; }
I have deployed this type of data using both solution #1 and the Entity Attribute Value model and I honestly prefer the latter.
We now have incorporated new data from multiple sources and the number of attributes has grown to around 1000. I don't see how deploying a table with 1000 columns can be considered a good solution.
Also, the data is used downstream in multiple places which means that If I had gone with solution #3 (which looks like the best one) we would have needed to edit tens of queries to add a new join with the new table for the class with custom attributes
My understanding is that when Stonebraker went back to academia after the commercially successful ingres database project. He had some interesting ideas on how to apply object orientated principals to the relational database.
The result ended up being the postgres database management system.
I heard jonskeet write a book so stackoverflow mod doesn't consider his answer as a opinion