'How to solve EagerLoadingError [SequelizeEagerLoadingError]: Table_B is not associated to Table_A?

I am trying to fetch data from Table_A and Table_B using node and sequelize

Table Structure

Table_A:
id PK
name Text

Table_B:
id PK
a_id FK_tableA_id
name Text

Model

TableA.js

'use strict';

const DataTypes = require('sequelize').DataTypes;

module.exports = (sequelize) => {
    const Table_A = sequelize.define('Table_A', {

        id: {
            type: DataTypes.UUID,
            allowNull: false,
            primaryKey: true
        },
        name: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    });

    Table_A.associate = models => {
        Table_A.belongsTo(models.Table_B, { as: 'tb' });
    }

    return Table_A;
};

TableB.js

'use strict';

const DataTypes = require('sequelize').DataTypes;

module.exports = (sequelize) => {
    const Table_B = sequelize.define('Table_B', {

        id: {
            type: DataTypes.UUID,
            allowNull: false,
            primaryKey: true
        },
        a_id: {
            type: DataTypes.UUID,
            allowNull: false,
            defaultValue: null
        },
        name: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    });

    return Table_B;
};

I am getting below error while I am trying to run the query using sequelize, Can you please guide me where I am making the mistake?

Error

EagerLoadingError [SequelizeEagerLoadingError]: Table_B is not associated to Table_A!
    at Function._getIncludedAssociation (C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:545:13)
    at Function._validateIncludedElement (C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:482:53)
    at C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:401:37
    at Array.map (<anonymous>)
    at Function._validateIncludedElements (C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:397:39)
    at Function.aggregate (C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:1204:12)
    at Function.count (C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:1252:31)
    at async Promise.all (index 0)
    at async Function.findAndCountAll (C:\Project\test\FilterTest\node_modules\sequelize\dist\lib\model.js:1268:27)

index.js

'use strict';

const { Op } = require('sequelize');
const {sequelize, connect } = require('./db');
const uninitModels = require('./models');
let initModels = uninitModels(sequelize);
initModels = { connection: sequelize, ...initModels }

const {
    Table_A, Table_B
} = initModels;

function dbCall(final) {
    Table_A.findAndCountAll(final).then((result)=>{
        console.log(result)
    }).catch((err)=>{
        console.log(err)
    })
}

function data() {
    let final = {
        include: [
            {
                model: Table_B,
                attributes: ['id', 'name', 'a_id'],
                as: 'tb'
            }
        ]
    }
    dbCall(final);
}

data();


Solution 1:[1]

I suppose you didn't register associations that should be registered by calling associate methods of models.
Also you confused how models are linked. If a model1 has a foreign key field pointing to a model2 then an association should be model1.belongsTo(model2).
In your case it should be:

Table_A.associate = models => {
        Table_A.hasMany(models.Table_B, { as: 'tb', foreginKey: 'a_id' });
    }

and in the model Table_B:

Table_B.associate = models => {
        Table_B.belongsTo(models.Table_A, { as: 'ta', foreginKey: 'a_id' });
    }

Pay attention to foreignKey option, you need to indicate it explicitly because your foreign key field is named other than Table_A+id.

Solution 2:[2]

I got stuck on this same error (for what seemed like an eternity) and finally realized that there was a big flaw in the way I was declaring my associations.

Incorrect:

    Account.associate = function (models) {
        Account.hasMany(models.History, {
            onDelete: "cascade"
        });
    };

    Account.associate = function (models) {
        Account.hasMany(models.User, {
            onDelete: "cascade"
        });
    };

In hindsight, this was a really silly oversight doing two declarations here. tl;dr the 2nd declaration was canceling out the 1st one.

Correct:

    Account.associate = function (models) {
        Account.hasMany(models.History, {
            onDelete: "cascade"
        });
        Account.hasMany(models.User, {
            onDelete: "cascade"
        });
    };

One declaration with multiple function calls for the win.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anatoly
Solution 2 cf512