如何在多对多关系中禁用复合键的唯一约束?
How to disable unique constraint on composite key in many-to-many relationship?
当 2 个模型通过联结 table 在 Sequelize 中以多对多关系相关联时,将根据 2 个模型的 2 个外键创建一个复合键。
在此处找到的续集文档中:https://sequelize.org/master/manual/advanced-many-to-many.html
它说可以强制 table 拥有 ID privateKey。
A 和 C 是多对多关系中的 2 个模型。结点table/model是B。
模型 A 和 C 定义与此示例无关。
//Defining the junction table B with the private key ID
sequelize.define('B', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
}
}, { timestamps: false });
A.belongsToMany(Profile, { through: B });
C.belongsToMany(User, { through: B });
即使将我的 ID 私钥添加到那个 table,那 2 个外键的唯一约束仍然存在。如何禁用该约束?
已找到答案
任何浏览网页寻找答案的人,很简单,您必须在 through 模型中传递 unique: false
选项。
A.belongsToMany(Profile, { through: { model: B, unique: false}});
C.belongsToMany(User, { through: { model: B, unique: false}});
当 2 个模型通过联结 table 在 Sequelize 中以多对多关系相关联时,将根据 2 个模型的 2 个外键创建一个复合键。 在此处找到的续集文档中:https://sequelize.org/master/manual/advanced-many-to-many.html 它说可以强制 table 拥有 ID privateKey。
A 和 C 是多对多关系中的 2 个模型。结点table/model是B。 模型 A 和 C 定义与此示例无关。
//Defining the junction table B with the private key ID
sequelize.define('B', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
}
}, { timestamps: false });
A.belongsToMany(Profile, { through: B });
C.belongsToMany(User, { through: B });
即使将我的 ID 私钥添加到那个 table,那 2 个外键的唯一约束仍然存在。如何禁用该约束?
已找到答案
任何浏览网页寻找答案的人,很简单,您必须在 through 模型中传递 unique: false
选项。
A.belongsToMany(Profile, { through: { model: B, unique: false}});
C.belongsToMany(User, { through: { model: B, unique: false}});