Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize, foreign keys as composite primary key

it is possible to define two foreign keys as a composite primary key of a model?

A user can only be a member of one family, a family can have many members and the family-members table need the references of the user and family

const User = sequelize.define(
    'User',
    {
        id: { type: dataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true },
        name: { type: dataTypes.STRING(30) },
        email: { type: dataTypes.STRING(30) }
        ...
    },
    {
        classMethods: {
            associate(models) {
                User.hasOne(models.FamilyMember, {
                    foreignKey: 'user_id'
                }
            }
        }
    }
)

const Family = sequelize.define(
    'Family',
    {
        name: { type: dataTypes.STRING(30) }
    },
    {
        classMethods: {
            associate(models) {
                Family.hasMany(models.FamilyMember, {
                    foreignKey: 'family_id'
                }
            }
        }
    }
)

const FamilyMember = sequelize.define(
    'FamilyMember',
    {
        name: { type: dataTypes.STRING(30) },
        /*
        family_id and user_id will be here after associations but I wanted them to be a composite primaryKey
        */
    }
)
like image 371
Sergio Flores Avatar asked Apr 27 '16 07:04

Sergio Flores


People also ask

How do you make a composite primary key in Sequelize?

You can create composite primary keys in Sequelize by specifying primaryKey: true against more than one column.

How do you set a foreign key in Sequelize?

Sequelize association methods also accept an options object that you can use to configure the details of the association. For example, you can change the foreign key name on the table by adding the foreignKey property: User. hasOne(Invoice, { foreignKey: "invoice_creator", // UserId -> invoice_creator });

How do I auto generate ID in Sequelize?

To add the AUTO_INCREMENT attribute into the columns of your tables with Sequelize, you need to add the autoIncrement attribute into your Sequelize Model definition. For example, suppose you want to add an auto increment id column into the Users table.


2 Answers

In fact, almost I got the solution from documentation:

User = sequelize.define('user', {});
Project = sequelize.define('project', {});
UserProjects = sequelize.define('userProjects', {
    status: DataTypes.STRING
});

User.belongsToMany(Project, { through: UserProjects });
Project.belongsToMany(User, { through: UserProjects });

By default the code above will add projectId and userId to the UserProjects table, and remove any previously defined primary key attribute - the table will be uniquely identified by the combination of the keys of the two tables, and there is no reason to have other PK columns.

Source

like image 70
Sergio Flores Avatar answered Sep 26 '22 01:09

Sergio Flores


For anyone looking to create a composite index primary key based of the columns(keys) in your join table when doing migrations. You will need to add a primary key constraint for the two columns that you wish to act as the combined primary key for the table.

module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.createTable('itemtags', {
      itemId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'items',
          key: 'id',
        },
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE',
        allowNull: false
      },
      tagId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'tags',
          key: 'id',
        },
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE',
        allowNull: false
      }
    })
      .then(() => {
        return queryInterface.addConstraint('itemtags', ['itemId', 'tagId'], {
          type: 'primary key',
          name: 'gametag_pkey'
        });
      });
  },
  down: function (queryInterface, Sequelize) {
    return queryInterface.dropTable('gametags');
  }
};

Which is roughly the same as doing ALTER TABLE ONLY my_table ADD CONSTRAINT pk_my_table PRIMARY KEY(column1,column2); in postgres.

like image 28
holmberd Avatar answered Sep 26 '22 01:09

holmberd