`is_a` relationship type
It would be great to have an
is_a
relationship, meaning each row in the source resource matches one and exactly one row in the destination resource, and has a matching primary key. Perhaps there is already an idiom for this?23 Replies
Yep! You can do
has_one
Which is at least an approxamation
something like:
Ah, I was actually thinking of it for the opposite case; in order to be able to provide inheritance.
well,
has_one
is just the name, but should be functionally equivalent if you're pointing at the same primary keyThe issue I think is the underlying table structure. All
Journal
s, Book
s and Magazine
s are Publication
s and other resources may be happy to interact with any sort of Publication
, but others might care only about Journal
s or Book
s.
Another standard pattern is to dump all the attributes into one table and add a type
column—and the associated validation headache—but this is not ideal in some cases.
Sharing by definition the same primary key has benefits in these cases.
In Postgres this is references
, same in Ecto.Well, if you do
belongs_to :something, Something
it will create a foreign key to that thing when generating migrations
And if you make it belongs_to, :something, Something, allow_nil?: false
then you're guaranteed for that other thing to existIf I add
primary_key :something_id
, will it produce references
?🤔 I'm not sure I understand
its the
belongs_to
that makes it add references
to migrations
There is another option that you can do with resources:
Then you can make as many resources that do that same thing as you want
and they will all share the same table (and the migration generator will generate migrations with the superset of all columns for that table)Right, in various SQL flavors, and definitely Postgres, you can do them simultaneously:
Yeah, so:
will give you what you want in that case
i.e "don't automatically define the attribute" and "use the attribute called
id
as the foreign key"
and then you will get a table w/ a primary key that references another tableOk, great! No
primary_key
required?well, I guess it depends
I didn't think it was implied by
belongs_to
.You can do
the snippet I gave you was assuming you were already defining the primary key elsewhere
but yeah if you want it to actually define the
id
attribute and make it a primary key, then:
They should be constrained to be identical by the database, simultaneously foreign key and primary key.
Ah, great.
Yeah, no matter how you cut it you will never get multiple attributes called
:id
its just wether or not you want it defined explicitly or implicitlySpecifically implicitly. ^^
I don't want it to have a separate id.
sorry, thats not what I meant
You're going to have to do the work to make the attribute equal the value that you want
The database shouldn't allow creation of rows in the child table without a matching row in the parent, by the
foreign key
(references
) constraint.that and
the two snippets I just showed are equivalent
what I mean by "explicit" vs "implicit" is just "does the
:id
attribute appear in the attributes
block or not"This one looks right.
not talking about the underlying database structure
they do exactly the same thing
Ah yeah, not in comparison to each other, the ones from before.
Cool, thanks very much!
no problem 😄