You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot have foreign key on same column as primary key #5769
If we define two tables with a 1 to 1 relation and a shared primary key, when the tables are generated, the primary key is not created on the "sub-table". The first table is a generic "Event" table and the second table represents more details of a specific kind of event.
The the db, both tables are generated with fields as expected, and a foreign key is placed on the GpsEvent table (event_id) however no primary key is generated on this table. If I define a separate field on this table for the primary key (eg. gps_event_id) and then both primary key (gps_event_id) and foreign key (event_id) are generated correctly. It appears sequelize doesn't generate the PK when an FK is defined on the same column.
Hmm, defining a foreign key should not kill the primary key property, but i guess it could happen.
If you have a _id column on both tables that refer to each other (or atleast referes to event from GpsEvent) then you'll need something like GpsEvent.belongsTo(Event, { foreignKey: '_id', targetKey: '_id' });
Thanks @mickhansen. The problem was that I was referring to the field name "event_id" in the association, rather than the model property name "_id". Once I changed this it worked as expected and I didn't need to explicitly add the targetKey attribute.
@mickhansen is there any documentation of the conceptual difference between what sequelize refers to as "fields" and "attributes" and which one corresponds to the actual column name, if either one does?
Activity
mickhansen commentedon Apr 20, 2016
Hmm, defining a foreign key should not kill the primary key property, but i guess it could happen.
If you have a
_idcolumn on both tables that refer to each other (or atleast referes to event from GpsEvent) then you'll need something likeGpsEvent.belongsTo(Event, { foreignKey: '_id', targetKey: '_id' });mjy78 commentedon Apr 20, 2016
Thanks @mickhansen. The problem was that I was referring to the field name "event_id" in the association, rather than the model property name "_id". Once I changed this it worked as expected and I didn't need to explicitly add the targetKey attribute.
I changed from
to
jedwards1211 commentedon May 2, 2018
@mickhansen is there any documentation of the conceptual difference between what sequelize refers to as "fields" and "attributes" and which one corresponds to the actual column name, if either one does?