【发布时间】:2014-06-11 14:02:51
【问题描述】:
我正在制作现有数据库的模型。我的表通过连接表具有多对多自引用。连接表有两个字段:“parent_id”和“child_id”。 我正在尝试这样定义关联:
company.hasMany(company, {as: 'parents', foreignKey: 'child_id', through: 'company_relation'});
company.hasMany(company, {as: 'children', foreignKey: 'parent_id', through: 'company_relation'});
但是当我尝试设置子公司时,Sequelize 仍在联结表中寻找“children_id”外键,而不是像我定义的那样寻找“child_id”:
company.setChildren([child]);
返回错误:
error: column company_relation.children_id does not exist
如果我不想以单数形式命名我的关联,如何在联结表中定义自定义外键,如下所示:
company.hasMany(company, {as: 'parent', foreignKey: 'child_id', through: 'company_relation'});
company.hasMany(company, {as: 'child', foreignKey: 'parent_id', through: 'company_relation'});
还有其他方法吗?谢谢,如果我很难理解我很抱歉:)
【问题讨论】: