【问题标题】:Is there a way to stop sequelize sync() from adding specific foreign keys?有没有办法阻止 sequelize sync() 添加特定的外键?
【发布时间】:2020-06-10 18:28:30
【问题描述】:

这是问题的要点。

我有一张桌子“盒子”。有多个项目表可以与一个框相关联(“Widgets”、“Dohickies”、“Thingamabobs”)。

我有一个关系表“ItemsInBox”,其中包含 BoxId、ItemId、itemType。

在我的模型中,我使用“ItemsInBox”作为“通过”表来创建关联(belongsToMany、hasMany),但没有与各种项目表关联的实际外键,因为它会导致外键冲突。所有这些都很好。

问题是当我为模型编写测试时,我使用 sequelize.sync() 来生成表。 Sync() 自动为所有关联添加外键。

我不能使用模型定义中的“references”属性来创建关联,因为“Boxes”表需要能够引用 3 个不同的表,但就我而言,references 属性不能是数组可以说。

有没有办法告诉 sequelize.sync() 跳过为特定关联添加外键?

注意:目前正在使用 sequelize v3,但正在升级到 v5。

【问题讨论】:

    标签: javascript mysql synchronization sequelize.js


    【解决方案1】:

    想出了如何做到这一点。

    在您的初始化脚本中,您必须遍历所有模型并执行 [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”,这将阻止辅助关联的执行。

    你在哪里使用sync():

    await sqldb.sequelize.sync({force: true }).then(() => {
      // create all of the associations that shouldn't have foreign keys associated
      sqldb.doNFKAssociations()
    })
    

    现在您在 sync() 方法运行后创建关联,并且您不会得到不需要的外键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2019-10-20
      相关资源
      最近更新 更多