Answer the question
In order to leave comments, you need to log in
Union with relational databases?
The task is to make a entity that can store different data. I would like to implement the Entity and Property model so that Property can store different types of data (similar to union c in C ++).
I see only one way, to create a property_*_values table (where * is the data type, like string, integer, float) and, based on the type in the properties table, do a dynamic JOIN (implementation through an inherited column). Perhaps there are other ways or ready-made solutions?
I use PostgreSQL, MongoDB had to be abandoned.
Answer the question
In order to leave comments, you need to log in
Slides for your question www.slideshare.net/stepanyuk/implementation-of-eav-pattern-for-activerecord-models-13263311
Well, there are a lot of ready-made solutions - you just have to choose
github.com/search?q=eav&repo=&langOverride=&start_value= 1&type=Repositories&language=Ruby
Or make one table with many columns
property | type | string value | int value | integer_value |…
and extract data depending on the contents of the type column
So I'm just wondering why you need such a model? if it `s not a secret.
Well, honestly, I worked with commercial engines, they solved this problem like this.
id | type | value
int | enum() | varchar(255) I
also see an approach of this type
id|value varchar(255)
And already programmatically determine the type, for example, in php there are methods is_integer (), is_float (), ..., is_array (), it will still get tired for the method for determining the date, emails, phone numbers, files… For this regular expressions will help.
But it seems to me it is more reasonable than to be wiser with a DB.
It seems to me that here the decision should be combined. The data itself can simply be serialized during storage and does not bother with different types and a set of columns. But then, to search by attributes, you will need to have some kind of separate server, for example, sphinx. H
Use database relationality. Try not to store the data itself in the same table, but only links to them through a polymorphic relationship.
table entities
storable_id, storable_type
and tables entity_integers, entity_floats, etc.
id, value (in the corresponding type)
ROR is good friends with such things.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question