我有一个没有任何主键的续集模型:
module.exports = (sequelize, DataTypes) => {
const usersDoors = sequelize.define('usersDoors',
{
user_uid: {
type: DataTypes.UUID,
allowNull: false,
},
door_uid: {
type: DataTypes.UUID,
allowNull: false,
},
property_manager_uid: {
type: DataTypes.UUID,
allowNull: true,
},
tenant_uid: {
type: DataTypes.UUID,
allowNull: true,
},
created_at: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
tableName: 'users_doors',
indexes: [
{
name: 'doors_users_indexes',
unique: true,
fields: ['user_uid', 'door_uid', 'property_manager_uid', 'tenant_uid'],
},
],
classMethods: {
associate: (models) => {
usersDoors.belongsTo(models.users, { foreignKey: 'user_uid' });
usersDoors.belongsTo(models.doors, { foreignKey: 'door_uid' });
usersDoors.belongsTo(models.propertyManagers, { foreignKey: 'property_manager_uid' });
usersDoors.belongsTo(models.tenants, { foreignKey: 'tenant_uid' });
},
},
});
return usersDoors;
};
当我插入数据时(通过sequelize),sequelize会向表中添加一个复合主键,其中包含前两列(user_uid和door_uid)。这打破了我的东西,我不想要它。
如何在插入数据时停止创建不需要的主键的续集?
更多详情:
最后,这是此模型的续集迁移:
const tableName = 'users_doors';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable(tableName, {
user_uid: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'users',
key: 'uid',
},
},
door_uid: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'doors',
key: 'uid',
},
},
property_manager_uid: {
type: Sequelize.UUID,
allowNull: true,
references: {
model: 'property_managers',
key: 'uid',
},
},
tenant_uid: {
type: Sequelize.UUID,
allowNull: true,
references: {
model: 'tenants',
key: 'uid',
},
},
created_at: {
type: Sequelize.INTEGER,
allowNull: false,
},
})
.then(() => {
return queryInterface.addIndex('users_doors',
['user_uid', 'door_uid', 'property_manager_uid', 'tenant_uid'],
{
indexName: 'doors_users_indexes',
indicesType: 'UNIQUE',
});
});
},
down: (queryInterface) => {
return queryInterface.dropTable(tableName)
.then(() => {
return queryInterface.removeIndex('users_doors', ['user_uid', 'door_uid', 'property_manager_uid', 'tenant_uid']);
});
},
};
答案 0 :(得分:2)
您只需要为续集模型添加属性,以及将作为主键的迁移
// in sequelize model definition
const usersDoors = sequelize.define('usersDoors',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
user_uid: {
type: DataTypes.UUID,
allowNull: false,
},
// other fields
// in the migrations file
return queryInterface.createTable(tableName, {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
user_uid: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: 'users',
key: 'uid',
},
},
// other fields
根据文档,当您创建M:M关系时,sequelize会自动创建一个主键,该主键由两个引用表创建关系的表的外键组成(users
和doors
在您的情况下)
该表将由两个表的键组合唯一标识
修改强>
假设您在belongsToMany
和users
模型上使用了doors
,则可以避免在unique index
上自动创建user_uid
和door_uid
。您需要在m:m关系初始化的unique: false
对象中指定through
条件(在两个模型中都要避免任何自动唯一约束)
// in users model
users.belongsToMany(
models.doors,
{
through: { model: models.usersDoors, unique: false },
foreignKey: 'user_uid'
}
);
// in doors model
doors.belongsToMany(
models.users,
{
through: { model: models.usersDoors, unique: false },
foreignKey: 'door_uid'
}
);