Sequelize复合唯一约束

时间:2016-01-07 20:47:59

标签: mysql node.js sequelize.js

定义模型:

export default function(sequelize, DataTypes) {
   return sequelize.define('Item', {
       minor: DataTypes.INTEGER,
       major: DataTypes.INTEGER,
   });
}

我可以将minor和major对定义为复合UNIQUE约束吗?

7 个答案:

答案 0 :(得分:17)

最近,在V4中,Sequelize将此添加到查询界面:

http://docs.sequelizejs.com/manual/tutorial/migrations.html#addconstraint-tablename-attributes-options-

queryInterface.addConstraint('Items', ['minor', 'major'], {
  type: 'unique',
  name: 'custom_unique_constraint_name'
});

答案 1 :(得分:9)

这是一个简单的答案:

major: { type: DataTypes.INTEGER, unique: 'compositeIndex'},
minor: { type: DataTypes.INTEGER, unique: 'compositeIndex'}

来源:http://docs.sequelizejs.com/en/latest/docs/models-definition/

如果它是联接表,您还可以通过belongsToMany关联创建唯一约束:

Major = sequelize.define('major', {})
Minor = sequelize.define('minor', {})

Major.belongsToMany(Project)
Minor.belongsToMany(User)

来源:http://docs.sequelizejs.com/en/v3/docs/associations/

就我而言,我希望找到一种方法来实现迁移。我通过在我的up函数结束时附加原始sql查询来完成此操作:

  up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Item', {
  major: {
    allowNull: false,
    type: Sequelize.INTEGER
  },
  minor: {
    allowNull: false,
    type: Sequelize.INTEGER
  },
})
.then(function() {
  return queryInterface.sequelize.query(
    'ALTER TABLE `Item` ADD UNIQUE `unique_index`(`major`, `minor`)'
  );
});

相关问题:

Sequelize, foreign keys as composite primary key

Unique constraint across foreign keys in Sequelize model

答案 2 :(得分:1)

对于ES6迁移

const item = {
  up: (queryInterface, Sequelize) => queryInterface.createTable('Items', {
    minor: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
    major: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
  }).then(() => queryInterface.addConstraint('Items', ['minor', 'major'], {
    type: 'unique',
    name: 'composite_key_name'
  })),
  down: queryInterface => queryInterface.dropTable('Items')
};

export default item;

答案 3 :(得分:0)

   queryInterface.createTable('Item', {
        minor: {
            type: Sequelize.INTEGER,
        },
        major: {
            type: Sequelize.INTEGER,
        }
    }, {
        uniqueKeys: {
            Items_unique: {
                fields: ['minor', 'major']
            }
        }
    });

答案 4 :(得分:0)

您可以使用以下内容:

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction(t => {
      return queryInterface.createTable('item',
        {
          minor: {
            type: Sequelize.INTEGER,
          },
          major: {
            type: Sequelize.INTEGER,
          }
        }, { transaction: t }
      ).then(() => {
        return queryInterface.addConstraint(
          'item',
          ['minor', 'major'],
          {
            type: 'unique',
            name: 'Items_unique'
          },
          {
            transaction: t
          }
        );
      });
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('item');
  }
}

答案 5 :(得分:0)

我通过使用以下方法解决了这个问题:


 await queryInterface.addConstraint('Item', {
      fields:  ['minor', 'major'],
      type: 'unique',
      name: 'unique_constraint_name'
    });

答案 6 :(得分:-2)

export default function(sequelize, DataTypes) {
   return sequelize.define('Item', {
       minor: {
        DataTypes.INTEGER,
        unique: true //add
       },
       major: {
        DataTypes.INTEGER,
        unique: true //add
       }
   });
}