如何解决 EagerLoadingError [SequelizeEagerLoadingError]: Table_B is not associated to Table_A?

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

我正在尝试使用节点和 [=33= 从 Table_ATable_B ]续集

Table结构

Table_A:
id PK
name Text

Table_B:
id PK
a_id FK_tableA_id
name Text

型号

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;
};

我在尝试使用 sequelize 运行 查询时出现以下错误,您能指导我哪里出错了吗?

错误

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

我想你没有注册应该通过调用模型的 associate 方法注册的关联。
您还混淆了模型的链接方式。如果 model1 有一个指向 model2 的外键字段,那么关联应该是 model1.belongsTo(model2).
在你的情况下应该是:

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

在模型中 Table_B:

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

注意foreignKey选项,你需要明确指出,因为你的外键字段不是Table_A+id.

我陷入了同样的错误(似乎是永恒),并最终意识到我声明关联的方式存在很大缺陷。

不正确:

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

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

事后看来,在这里做两个声明是一个非常愚蠢的疏忽。 tl;博士,第二个声明取消了第一个声明。

正确:

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

一个声明,多个函数调用为胜利。