I am designing a database using Postgres. For my use case, I need a modular data model.…
- User
I am designing a database using Postgres. For my use case, I need a modular data model. Specifically, I need to store Entities of different types. All Entities have a set of common fields, and then they have additional properties depending on their type. For instance, suppose that Entities represent Events. All Events have a title and a description. But if an Event is of type Comment, it will also have additional fields like topic and number of likes. My main requirements are as follows:
- I need to enforce NOT NULL constraints for the type-specific columns
- The data model should be easily extendable. Adding a new type of Entity should be easy, and require no downtime for the database
- In my application, I will have a view showing all Entities, sorted by date. So I need to be able to query all Entities, independently of their type, in a fast and efficient way
Can you explain to me a design pattern that can be used to achieve these requirements? Please describe it with a concrete example.
- Assistant
- User
Why is it difficule to maintain? I'm interested in EAV but I need to understand the impact.
- Assistant
To explain this, I will simplify the table schema a bit. Let's suppose we INSERT the following entries into the
entitiestable:codeINSERT INTO entities (type, attribute_key, attribute_value) VALUES ('user', 'id', 'user1'), ('user', 'username', 'Foobar') ('comment', 'id', 'comment1'), ('comment', 'topic', 'My Comment Topic'), ('comment', 'author', 'user1');In a traditional relational model where the tables
user => id, usernameandcomment => id, author (user.id), topicare given, we could JOIN ON user.id = comment.author. In this EAV approach, such an easy join is not possible.Another problem with this approach is that it is not clearly documented what attributes (keys) an entity has. This makes apps that use the database more error prone, since they could query undefined data more often. The developer needs to spend more time investigating what attributes belong to an entity.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)