Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled rejection TypeError: Dependency name must be given as a not empty string

I got this error when codes hitting require('./models').sequelize.sync(). (models is a directory created by running command sequelize init) Could anyone give me some hints about what induces this error?

> 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)

I'm using [email protected] with HapiJS. An similar repo can be found here. (This repo is indeed working, even with sequelize upgraded to latest version)

like image 257
zachguo Avatar asked Dec 01 '22 17:12

zachguo


2 Answers

It was due to declaring of foreign key according to previous version of sequelize.

As per latest version "sequelize": "^4.31.2", correct way to add foreign key is-

addressId: {
    type: DataTypes.INTEGER,
    references: {
        model: 'addresses',
        key: 'id'
    }
}

I was declaring it as

addressId: {
    type: DataTypes.INTEGER,
    references: 'addresses',
    referencesKey: 'id'
}
like image 134
Varun Kumar Avatar answered May 25 '23 17:05

Varun Kumar


It's caused by a illformed self-referencing key in one one of my model file.

like image 43
zachguo Avatar answered May 25 '23 18:05

zachguo