sequelize: TypeError: Cannot read property 'getTableName' of undefined
What you are doing?
I’m attempting to create an association via an api interface that I have created.
What do you expect to happen?
It should’ve just found the user and continued on, but it seems the include is causing an issue, however just calling the “find” method from other tests appears to work just fine, so I’m not really sure what is going on.
What is actually happening?
I’m getting a Sequelize error that looks like to be failing when the table name is retrieved from the included models.
sentinel:api:user TypeError: Cannot read property 'getTableName' of undefined
at validateIncludedElement (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:505:27)
at C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:432:29
at Array.map (native)
at validateIncludedElements (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:428:37)
at null.<anonymous> (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:1364:32)
at tryCatcher (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:502:31)
at Promise._settlePromise (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:559:18)
at Promise._settlePromise0 (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:604:10)
at Promise._settlePromises (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:683:18)
at Async._drainQueue (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\async.js:138:16)
at Async._drainQueues (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\async.js:148:10)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\async.js:17:14)
at processImmediate [as _immediateCallback] (timers.js:383:17) +86ms
The error originates from this test, however it’s actually thrown here from here.
Dialect: mysql Database version: 10.1.8-MariaDB Sequelize version: 3.23.3
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 15 (5 by maintainers)
To add on to this I ran into this similar situation. Same error, same console logging results. My issue was
that there was no join table that ‘through’ could use, so just getting rid of ‘through’ like this:
Solved my issue
Solved
The issue was that I was passing ‘include’ through as an object and not an array.
Howcome there is no visibility into what model Sequelize is actually trying to access here? Why doesn’t it say something like
call to Model.getTableName() failed
?Most likely the model you are trying to include is undefined. Please add some code that reproduces the issue
In the
associate
function, make sure to add the through model using a simple string and not the model object. That was the issue in my case.You made my day man, thanks a lot. This solved my issue 😉
Had the same error message, but it turns out it was because the model I was trying to include had multiple associations to the model I was searching on. Adding an alias to the associations fixed the issue.
I just ran into this myself. This could really use a better error message, the one provided makes no sense.
@janmeier I doubt it, I added
console.log(options.include.AuthorizationPolicy.getTableName());
here and it showed the table name as expected, however the query still failed to getTableName.It’s either an issue with Sequelize or it has something to do with the way my application is currently loading models.
In regards to code that reproduces the error, could you not just clone the repository I provided?