javascript - 有没有办法阻止 sequelize sync() 添加特定的外键?
问题描述
这是问题的要点。
我有一张桌子“盒子”。有多个项目表可以与一个框相关联(“Widgets”、“Dohickies”、“Thingamabobs”)。
我有一个关系表“ItemsInBox”,其中包含 BoxId、ItemId、itemType。
在我的模型中,我使用“ItemsInBox”作为“通过”表来创建关联(belongsToMany、hasMany),但是没有与各种项目表关联的实际外键,因为它会导致外键冲突。所有这些工作正常。
问题是当我为模型编写测试时,我使用 sequelize.sync() 来生成表。Sync() 自动为所有关联添加外键。
我不能使用模型定义中的“references”属性来创建关联,因为“Boxes”表需要能够引用 3 个不同的表,但据我所知,references 属性不能是数组。
有没有办法告诉 sequelize.sync() 跳过为特定关联添加外键?
注意:目前正在使用 sequelize v3,但正在升级到 v5。
解决方案
想出了如何做到这一点。
在您的初始化脚本中,您必须遍历所有模型并执行 [model].associate(models)。
就像是
const tableNames = Object.keys(models)
for (let modelName of tableNames) {
let model = models[modelName]
if (typeof model.associate === 'function') model.associate(db)
}
在您的模型中,您有一个关联功能。
在 v3/v4 中
classMethods: {
associate: function (models) {
...
}
}
在 v5 中
[model].associate(){
}
或者,如果您使用类定义而不是定义,则您有一个关联属性。
创建第二个函数来创建非外键关联。
在 v3/v4 中
classMethods: {
associate: function (models) {
...
},
nfkAssociate: function (models) {
[model].belongsTo(...)
}
}
在 v5 中
[model].nfkAssociate = function (models) {
[model].belongsTo(...)
}
然后在你的初始化脚本中
const tableNames = Object.keys(models)
for (let modelName of tableNames) {
let model = models[modelName]
if (typeof model.associate === 'function'){
model.associate(db)
}
}
db.doNFKAssociations = function () {
for (let modelName of tableNames) {
const model = db[modelName]
if (model.nfkAssociate) {
model.nfkAssociate(db)
}
}
}
if (!config.delayNFKAssociations) {
db.doNFKAssociations()
}
默认情况下,您会立即运行 associate() 和 nfkAssociate()。如果您处于运行自动化测试等情况,您可以在初始化配置中添加一个额外的属性“delayNFKAssociations”,这将阻止辅助关联的执行。
你在哪里使用同步():
await sqldb.sequelize.sync({force: true }).then(() => {
// create all of the associations that shouldn't have foreign keys associated
sqldb.doNFKAssociations()
})
现在,您正在 sync() 方法运行后创建关联,并且您不会获得不需要的外键。
推荐阅读
- javascript - 如何在 json 对象中使用变量?
- python - Python:双向变量链接?
- keras - 如何启用 AMD Radeon 显卡来训练深度学习模型?
- javascript - 如何更优雅地链式调用 useEffect 中的函数
- cmake - 未定义的符号仍然存在,但共享库可以编译并且似乎可以正常运行
- python - Heroku python'应用程序错误'(“应用程序发生错误,无法提供您的页面”)
- mongodb - 带有内部选择的 Mongo 查询(在 MongoDB Compass 过滤器中)
- winapi - 重复调用 WriteConsole(Win64 上的 NASM x64)
- c++ - C++ 原子获取/释放操作的实际含义
- azure - 我们可以在一个批处理作业中拥有多个阅读器(通过分页读取记录)吗