S
S
sdevalex2012-06-16 10:52:10
Ruby on Rails
sdevalex, 2012-06-16 10:52:10

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

6 answer(s)
S
solenko, 2012-06-19
@sdevalex

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

P
Pavel Isakov, 2012-06-16
@kwikpik

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

A
Alexey Firsov, 2012-06-16
@lesha_firs

So I'm just wondering why you need such a model? if it `s not a secret.

A
Alexey Firsov, 2012-06-16
@lesha_firs

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.

@
@mgyk, 2012-06-16
_

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

D
dizer, 2012-06-16
@dizer

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 question

Ask a Question

731 491 924 answers to any question