我在应该关联的两个表上运行一个简单的查询,但是我不断收到以下消息:
item_translation与项目无关!
基本上item_translation
应该属于item
。根据{{3}},我已正确完成了所有操作。这是模型:
ItemTranslation模型:
module.exports = function () {
return function (app) {
/** @type {Sequelize.Sequelize} */
const sequelize = app.get('sequelize')
const ItemTranslation = sequelize.define('item_translation', {
_id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
sourceId: {
type: Sequelize.STRING,
allowNull: false,
references: {
model: 'item',
key: '_id'
},
onDelete: 'cascade',
onUpdate: 'cascade'
},
text: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true
})
ItemTranslation.associate = function () {
ItemTranslation.belongsTo(sequelize.models.item)
}
}
}
项目模型:
module.exports = function () {
return function (app) {
/** @type {Sequelize.Sequelize} */
const sequelize = app.get('sequelize')
sequelize.define('item', {
_id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
text: {
type: Sequelize.TEXT,
allowNull: true
}
}, {
freezeTableName: true
})
}
}
我错过了什么吗?
答案 0 :(得分:0)
我不确定,但是尝试一下。我总是明确定义外键。像这样的东西。
ItemTranslation.belongsTo(sequelize.models.item,{foreignKey:'sourceId'})
Item.associate = function () {
Item.hasMany(sequelize.models.ItemTranslation,{foreignKey:'sourceId'})
}