Skip to content

TypeError: Cannot read property 'getTableName' of undefined #6127

Closed

Description

@xKrvZ

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

Activity

  1. janmeier commented on Jun 18, 2016

    @janmeier
    Member

    Most likely the model you are trying to include is undefined. Please add some code that reproduces the issue

  2. xKrvZ commented on Jun 18, 2016

    @xKrvZ
    ContributorAuthor

    @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?

  3. xKrvZ commented on Jun 18, 2016

    @xKrvZ
    ContributorAuthor

    Okay, so after a little debugging of my own, I think it has something to do with the way my associations are setup, my models are apparent when I associate them with each other, so I'm not 100% sure.

    So what I did was add this line include = include[Object.keys(include)[0]]; here, and it then gave me this error:

      sentinel:api:user Error: authorization_user_policies is not associated to AuthorizationPolicy!
        at validateIncludedElement (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:552:11)
        at C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:434:29
        at Array.map (native)
        at validateIncludedElements (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:430:37)
        at validateIncludedElement (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:627:30)
        at C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:434:29
        at Array.map (native)
        at validateIncludedElements (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:430:37)
        at null.<anonymous> (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\lib\model.js:1366: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:504:31)
        at Promise._settlePromise (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:561:18)
        at Promise._settlePromise0 (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:606:10)
        at Promise._settlePromises (C:\Users\Kieron\Dracade\web\sentinel\node_modules\sequelize\node_modules\bluebird\js\release\promise.js:685: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) +96ms
    

    It's stating that my authorization_user_policy table is not associated to AuthorizationPolicy, but they are associated according to the SQL schema.

  4. xKrvZ commented on Jun 18, 2016

    @xKrvZ
    ContributorAuthor

    Solved

    The issue was that I was passing 'include' through as an object and not an array.

  5. justechn commented on Nov 1, 2016

    @justechn

    I just ran into this myself. This could really use a better error message, the one provided makes no sense.

  6. janmeier commented on Nov 1, 2016

    @janmeier
    Member

    @justechn I understand your sentiment, but having to monkeypatch type checking of all arguments ( and their arbitrarily nested children) is not feasible in my mind

  7. trevorh2007 commented on Mar 14, 2018

    @trevorh2007

    To add on to this I ran into this similar situation. Same error, same console logging results. My issue was

    include: [{
        model: Scholarship,
        as: 'scholarships',
        required: false,
        through: {
            attributes: []
        }
    }]
    

    that there was no join table that 'through' could use, so just getting rid of 'through' like this:

    include: [{
        model: Scholarship,
        as: 'scholarships',
        required: false
    }]
    

    Solved my issue

  8. heisian commented on Mar 20, 2018

    @heisian

    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?

  9. calintamas commented on Aug 29, 2018

    @calintamas

    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.

    User.belongsToMany(models.project, { through: 'user_project', foreignKey: 'user_id' });
  10. camilodelvasto commented on Sep 2, 2018

    @camilodelvasto

    I was getting the error with this line:

    include: [{ all: false}]

    When I set it to true, the error went away. Is that error expected or is it something in my setup? Also, I couldn't find it documented... just leaving it here in case it helps someone, but it seems the error message is indeed misleading.

  11. TyroTan commented on Nov 25, 2018

    @TyroTan

    It's amazing how I also bumped into this but it's because 'model' was misspelled as 'mode'. I thought that it's worthy to mention

  12. mahsan3-zz commented on Mar 28, 2019

    @mahsan3-zz

    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.

  13. davicente commented on Aug 13, 2019

    @davicente

    To add on to this I ran into this similar situation. Same error, same console logging results. My issue was

    include: [{
        model: Scholarship,
        as: 'scholarships',
        required: false,
        through: {
            attributes: []
        }
    }]
    

    that there was no join table that 'through' could use, so just getting rid of 'through' like this:

    include: [{
        model: Scholarship,
        as: 'scholarships',
        required: false
    }]
    

    Solved my issue

    You made my day man, thanks a lot. This solved my issue ;)

  14. michcio1234 commented on Dec 30, 2019

    @michcio1234

    I just ran into it because I misspelled association.
    This error message really could be a bit more useful. ;/

  15. LWanjiru commented on Sep 21, 2023

    @LWanjiru

    Hope this helps. I was stuck too long trying to figure it out after I got major bugs on a project. Here's the link to how I solved it ==> #11285 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions