我正在对两个本应关联的表运行一个简单的查询,但我一直收到以下消息:
item_translation未与项关联!
基本上item_translation
应该属于item
。根据医生的说法,我做的每件事都是正确的。以下是型号:
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
})
}
}
我错过什么了吗?
我不确定,但试试这个。我总是明确定义外键。像这样的事情。
ItemTranslation.belongsTo(sequelize.models.item,{foreignKey:'sourceId'})
Item.associate = function () {
Item.hasMany(sequelize.models.ItemTranslation,{foreignKey:'sourceId'})
}