Jeet Mehta
Jeet Mehta

Reputation: 1

How do I add a composite foreign key in sequelize?

I have defined the following tables, archive and infos. Each file can have many information tags. The combination of field and tag is unique. For this I require a composite primary key consisting of field and tag. field refers to field in the archive table. The model definitions are given below.

Archive table:

    module.exports = (sequelize, DataTypes) => {
      let archive = sequelize.define('archive', {
        fileid: {
          type: DataTypes.STRING,
          primaryKey: true,
          allowNull: false
        },
        filename: {
          type: DataTypes.STRING,
          unique: false,
          allowNull: false
        },
        originalname: {
          type: DataTypes.STRING,
          unique: false,
          allowNull: false
        },
        downloadlink: {
          type: DataTypes.STRING,
          unique: false,
          allowNull: false
        },
        domain: {
          type: DataTypes.STRING,
          unique: false,
          allowNull: false
        },
        sem: {
          type: DataTypes.INTEGER,
          unique: false,
          allowNull: false
        },
        branch: {
          type: DataTypes.STRING,
          unique: false,
          allowNull: false
        }
      });
      archive.associate = models => {
        models.archive.hasMany(models.info, {
          foreignKey: 'fileid'
        });
        models.archive.hasMany(models.upvotes, {
          foreignKey: 'fileid'
        });
      };
      return archive;
    };

Info Table

    module.exports = (sequelize, DataTypes) => {
      let info = sequelize.define('info', {
        tag: {
          type: DataTypes.STRING,
          allowNull: false,
          primaryKey: true
        }
      });
      info.associate = models => {
        models.info.belongsTo(models.archive, {
          foreignKey: 'fileid',
          primaryKey: true
        });
      };
      return info;
    };

Making the primaryKey: true did not work. I have tried through: as well. I cannot seem to make it work.

Upvotes: 0

Views: 671

Answers (1)

Michael Nelles
Michael Nelles

Reputation: 6002

Sequelize can be a giant pain. I follow the docs to the letter and still things don't work with some of the more complex queries, such as can be the case with composite primary key. My suggestion to you is when you bump in to Model fails, go with Raw queries. That being said still take the extra steps to protect against SQL injection attacks.

here is an example:

  1. ensure that you include type
  2. santize variables (in this case code)
   db.sequelize
      .query('SELECT count(*) FROM logs WHERE code = :code ', {
         replacements: {
            code: code
         },
         type: Sequelize.QueryTypes.SELECT
      })
      .then((data) => {
        ...
      })
      .catch((err) => {
       ...
      });

I would have posted this a comment but there was not enough space.

Upvotes: 1

Related Questions