Node.js 续集:在第一次(成功)尝试后,具有多态关系的belongstomy在作用域上失败
我们正在运行:Node.js 续集:在第一次(成功)尝试后,具有多态关系的belongstomy在作用域上失败,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我们正在运行: nodejsv13.11 续集版本5.22.3 博士后v11.7-2 我有3个模型,GameVersion、Tag和TagGable(表示关联实体)。一个GV可以有许多标记,并且一个标记可以与许多GV(或通过TagGable的任何其他模型)关联 模型是基于此构建的(未显示多态性后处理和清理的挂钩): 游戏版本: this.belongsToMany(models.Tag, { through: { model: models.TagTaggable,
- nodejsv13.11
- 续集版本5.22.3
- 博士后v11.7-2
this.belongsToMany(models.Tag, {
through: {
model: models.TagTaggable,
unique: false,
scope: {
taggableType: 'game_version',
},
},
as: 'tags',
foreignKey: 'taggable_id',
constraints: false,
});
this.hasMany(models.TagTaggable, {
foreignKey: 'taggable_id',
scope: {
taggableType: 'game_version',
},
});
this.belongsToMany(models.GameVersion, {
through: {
model: models.TagTaggable,
unique: false,
scope: {
taggableType: 'game_version',
},
},
as: 'gameVersions',
foreignKey: 'tag_id',
constraints: false,
});
this.hasMany(models.TagTaggable, {
scope: {
taggableType: 'game_version',
},
});
this.belongsTo(models.Tag, {
as: 'tag',
foreignKey: 'tag_id',
});
// Polymorphic relationships
this.belongsTo(models.GameVersion, {
foreignKey: 'taggable_id',
constraints: false,
as: 'gameVersion',
});
标记:
this.belongsToMany(models.Tag, {
through: {
model: models.TagTaggable,
unique: false,
scope: {
taggableType: 'game_version',
},
},
as: 'tags',
foreignKey: 'taggable_id',
constraints: false,
});
this.hasMany(models.TagTaggable, {
foreignKey: 'taggable_id',
scope: {
taggableType: 'game_version',
},
});
this.belongsToMany(models.GameVersion, {
through: {
model: models.TagTaggable,
unique: false,
scope: {
taggableType: 'game_version',
},
},
as: 'gameVersions',
foreignKey: 'tag_id',
constraints: false,
});
this.hasMany(models.TagTaggable, {
scope: {
taggableType: 'game_version',
},
});
this.belongsTo(models.Tag, {
as: 'tag',
foreignKey: 'tag_id',
});
// Polymorphic relationships
this.belongsTo(models.GameVersion, {
foreignKey: 'taggable_id',
constraints: false,
as: 'gameVersion',
});
taggable:
this.belongsToMany(models.Tag, {
through: {
model: models.TagTaggable,
unique: false,
scope: {
taggableType: 'game_version',
},
},
as: 'tags',
foreignKey: 'taggable_id',
constraints: false,
});
this.hasMany(models.TagTaggable, {
foreignKey: 'taggable_id',
scope: {
taggableType: 'game_version',
},
});
this.belongsToMany(models.GameVersion, {
through: {
model: models.TagTaggable,
unique: false,
scope: {
taggableType: 'game_version',
},
},
as: 'gameVersions',
foreignKey: 'tag_id',
constraints: false,
});
this.hasMany(models.TagTaggable, {
scope: {
taggableType: 'game_version',
},
});
this.belongsTo(models.Tag, {
as: 'tag',
foreignKey: 'tag_id',
});
// Polymorphic relationships
this.belongsTo(models.GameVersion, {
foreignKey: 'taggable_id',
constraints: false,
as: 'gameVersion',
});
标签通过以下方式应用于GV:
await gv.setTags(metadata.tags);
其中metadata.tags
是标记模型的集合
在我第一次运行它时,它工作得非常好,我可以看到,在SequelizebelongAsmany.updateaAssociations
方法的内部进行调试,通过查看belongAsmany对象上的This.through.scope
,第一次的范围是正确的
我得到:
{taggableType: 'game_version'}
请注意驼峰壳键,它应该是这样的
这将导致以下查询:
INSERT INTO "tag_taggable" ("taggable_type","created_at","updated_at","taggable_id","tag_id") VALUES ('game_version','2020-11-27 18:49:34.767 +00:00','2020-11-27 18:49:34.767 +00:00',82,29)
任何后续尝试都会出现问题(这意味着它在启动服务器后第一次工作,但在重新启动服务器之前的任何尝试都会失败),我可以看到this.through.scope
现在会导致:
{taggable_type: 'game_version'}
注意蛇壳钥匙
这将导致以下查询(注意缺少“标记类型”列):
并抛出“非空约束冲突”
这似乎是Sequelize的底蕴,但我无法想象它是否已经浮出水面(除非多态m:n真的不常见)
有没有人有过这样的经历,和/或有人能解释一下这里发生了什么
我真的很想使用setTags
magic方法,但我现在正着手构建可标记的对象并将它们填充到数据库中
TIA提供任何见解/帮助
克里斯