We are currently building a PostgreSQL database that allows us to create profiles for hardware. Each profile exists of a few different properties which are all dynamically added to the profile through the UI. Each property can be of a different type (string, integer, text, …)
How would I most efficiently store these properties in a relational database?
I’ve read Storing diverse value types in a database and I’m charmed by the single answer there but I’m not at all confident that this is the ideal solution. Are there any pros/cons using the separate-table-per-type approach over the other solutions? Anyone has real world experience with this and minds shedding some light on the best approach?
My preferred way is single table with both key and value being varchar and/or text.
The only big disadvantage of table-per-type in that comment I can think of is that it’s hard to constraint the uniqueness of the key in pair with the entity this key-value entry belongs to.
Table inheritance (but still with table-per-type) would be another way to go and IMHO it’s brutally type-safe and compliant with postgres and ORM concepts:
This perfectly maps to JPA.