Skip to content

Cannot have foreign key on same column as primary key #5769

Closed

Description

@mjy78

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.

I define the model as follows:

var Event = sequelize.define('Event', {
    _id: {
      field: 'event_id',
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },    
    eventTypeId: {
        field: 'event_type_id',
        type: DataTypes.INTEGER,
        allowNull: false
    },
    time: {
        field: 'event_time',
        type: DataTypes.DATE,
        allowNull: false
    }
});

var GpsEvent = sequelize.define('GpsEvent', {
    _id: {
      field: 'event_id',
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true
    },    
    lat: {
        field: 'lat',
        type: DataTypes.DOUBLE,
        allowNull: false
    },
    lon: {
        field: 'lon',
        type: DataTypes.DOUBLE,
        allowNull: false
    }
});

GpsEvent.belongsTo(Event, { foreignKey: 'event_id' });

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.

Dialect: postgres
Database version: 9.4
Sequelize version: 3.5.1

Activity

  1. mickhansen commented on Apr 20, 2016

    @mickhansen
    Contributor

    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' });

  2. mjy78 commented on Apr 20, 2016

    @mjy78
    ContributorAuthor

    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

    GpsEvent.belongsTo(Event, { foreignKey: 'event_id' });
    

    to

    GpsEvent.belongsTo(Event, { foreignKey: '_id' });
    
  3. jedwards1211 commented on May 2, 2018

    @jedwards1211
    Contributor

    @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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions