我拥有这个模型。
'use strict'
var Sequelize = require('sequelize');
var db = require('../../config/database');
const TransactionModel = db.define('transactions', {
id : {
type: Sequelize.INTEGER,
primaryKey: true
},
guid: {
type: Sequelize.STRING
},
});
module.exports = TransactionModel
我拥有第二个模型
'use strict'
var Sequelize = require('sequelize');
var db = require('../../config/database');
const transactionModel = require('../models/transactionModel')
const accessCredentialsApiModel = db.define('transactions_items', {
guid: {
type: Sequelize.STRING
},
transaction_id : {
field : 'transaction_id',
type: Sequelize.INTEGER,
references: 'transactions',
key: 'id'
},
created_at : {
type : Sequelize.DATE
},
updated_at : {
type : Sequelize.DATE
},
qtd_event : {
type: Sequelize.INTEGER
},
amount: {
type: Sequelize.DECIMAL(10, 2)
}
});
transactionModel.hasMany(accessCredentialsApiModel);
module.exports = accessCredentialsApiModel
我通过将外键放在第二个模型中来为外键关系使用关联。但是是时候进行下面的查询了。
let transactions = await transactionModel.findAll({
where: {
entity_id: arrayEntities[i].id,
},
include: [
{ model: transactionsItemsModel}
],
raw: true
});
console.log(transactions)
我收到错误消息
SequelizeDatabaseError: Unknown column 'transactions_items.transactionId' in 'field list'
我找不到解决问题的方法,有人可以帮助我吗?
答案 0 :(得分:0)
该错误表明它正在寻找transactionId
,但您定义的是transaction_id
。您可以像这样告诉sequelize什么是外键和源键
transactionModel.hasMany(accessCredentialsApiModel, {foreignKey: 'id', sourceKey: 'transaction_id'});