代码命中require('./models').sequelize.sync()
时出现此错误。 (models
是通过运行命令sequelize init
创建的目录)任何人都可以给我一些关于导致此错误的提示吗?
> node src/server.js
Unhandled rejection TypeError: Dependency name must be given as a not empty string
at /Users/syg/Repos/example/node_modules/sequelize/node_modules/toposort-class/toposort.js:37:31
at Array.forEach (native)
at Toposort.self.add (/Users/syg/Repos/example/node_modules/sequelize/node_modules/toposort-class/toposort.js:35:22)
at /Users/syg/Repos/example/node_modules/sequelize/lib/model-manager.js:89:12
at Array.forEach (native)
at ModelManager.forEachModel (/Users/syg/Repos/example/node_modules/sequelize/lib/model-manager.js:58:15)
at /Users/syg/Repos/example/node_modules/sequelize/lib/sequelize.js:862:23
at tryCatcher (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/util.js:24:31)
at Promise._settlePromiseFromHandler (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:454:31)
at Promise._settlePromiseAt (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:530:18)
at Promise._settlePromiseAtPostResolution (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:224:10)
at Async._drainQueue (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/async.js:182:12)
at Async._drainQueues (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/async.js:187:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/syg/Repos/example/node_modules/sequelize/node_modules/bluebird/js/main/async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:358:17)
我将sequelize@3.2.0
与HapiJS
一起使用。可以找到类似的回购here。 (即使sequelize
已升级到最新版本,此仓库确实有效)
答案 0 :(得分:4)
它是由我的模型文件中的一个错误的自引用键引起的。
答案 1 :(得分:2)
这是因为根据之前版本的sequelize声明了外键。
根据最新版本" sequelize":" ^ 4.31.2",添加外键的正确方法是 -
addressId: {
type: DataTypes.INTEGER,
references: {
model: 'addresses',
key: 'id'
}
}
我宣称它是
addressId: {
type: DataTypes.INTEGER,
references: 'addresses',
referencesKey: 'id'
}