我正在尝试使用Sequelize作为ORM从数据库获取其ID。我在belongsTo协会中设置了模型。所以1个场地可以有1个电话号码和1个地址。我确实阅读了他们网站上的Sequelize文档,但没有找到解决此问题的方法。下面我向您提供了查询,错误消息以及模型相互连接的方式。
这是查询:
function findById(id) {
return this.findOne({
where: { id: id },
include: [
{
model: models.Address,
as: "venue_address"
},
{
model: models.PhoneNumber,
as: "phone_number"
}
]
}).then((venue) => {
if (!venue) {
return utils.createError(404, `Venue with id ${id} does not exist`);
}
return venue;
});
}
Sequelize返回以下错误:
TypeError: Cannot read property 'getTableName' of undefined
at Function._validateIncludedElement (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:465:30)
at options.include.options.include.map.include (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:395:37)
at Array.map (<anonymous>)
at Function._validateIncludedElements (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:390:39)
at Promise.try.then.then (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:1576:14)
at tryCatcher (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:756:18)
at tryOnImmediate (timers.js:717:5)
at processImmediate [as _immediateCallback] (timers.js:697:5)
Unhandled rejection Error: Transaction cannot be rolled back because it has been finished with state: commit
at Transaction.rollback (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/transaction.js:85:35)
at models.Address.createAddress.then.catch (/Users/kevin/Tabbs/API-Mysql/server/routes/venue.js:57:13)
at tryCatcher (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:689:18)
at Async._drainQueue (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:756:18)
at tryOnImmediate (timers.js:717:5)
at processImmediate [as _immediateCallback] (timers.js:697:5)
模型如下关联:
models.Venue.belongsTo(models.PhoneNumber, {
as: "phone_number",
onDelete: "CASCADE",
foreignKey: { allowNull: false }
});
models.Venue.belongsTo(models.Address, {
as: "venue_address",
onDelete: "CASCADE",
foreignKey: { allowNull: false }
});
当我不包含任何内容时,这就是我收到的内容:
感谢解决此问题的所有帮助!
答案 0 :(得分:1)
我认为您的表格正在按顺序初始化。如果你在这里看到我的例子:Example Index File你会注意到我有一个特定顺序的模型数组,我循环来初始化表。我认为你有一个外键依赖,在创建关联之前没有被初始化,这就是你看到这个错误的原因。
简短回答:您需要确保在依赖关系之前初始化外键依赖关系。