简体   繁体   English

如何在节点JS中关联两个模型

[英]How to associated two models in node JS

I'm trying to make this connection between these two tables, but I'm not getting it and it's giving the following error: "UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: cfg_user is not associated to dn_coluna!"我试图在这两个表之间建立这种连接,但我没有得到它,它给出了以下错误:“UnhandledPromiseRejectionWarning:SequelizeEagerLoadingError:cfg_user 与 dn_coluna 无关!”

follows below the controllers and models that I am using to inner join下面是我用于内部连接的控制器和模型

This is my cfg_user.js这是我的 cfg_user.js

const sequelizePaginate = require("sequelize-paginate");

module.exports = function(sequelize, DataTypes) {
    const cfg_user = sequelize.define("cfg_user", {
        'id_cfg_user': {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        'id_cfg_grupo': {
            type: DataTypes.INTEGER(11),
            allowNull: true
        },
        'nome': {
            type: DataTypes.CHAR(100),
            allowNull: true
        },
        'email': {
            type: DataTypes.CHAR(100),
            allowNull: true
        },
        'arquivo': {
            type: DataTypes.CHAR(255),
            allowNull: false
        },
        'senha': {
            type: DataTypes.CHAR(32),
            allowNull: true
        }
    },{
        tableName: "cfg_user",
        freezeTableName: true,
        timestamps: false
    });

    sequelizePaginate.paginate(cfg_user);
    return cfg_user;
};

This is my dn_coluna.js这是我的 dn_coluna.js

const sequelizePaginate = require("sequelize-paginate");
const cfg_user = require("./cfg_user");

module.exports = function(sequelize, DataTypes) {
    const dn_coluna = sequelize.define("dn_coluna", {
        'id_dn_coluna': {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        'id_cfg_user': {
            type: DataTypes.INTEGER(10),
            allowNull: false
        },
        'coluna': {
            type: DataTypes.CHAR(255),
            allowNull: false
        },
        'frase': {
            type: DataTypes.CHAR(255),
            allowNull: true,
        },
        'resumo': {
            type: DataTypes.CHAR(255),
            allowNull: true,
        },
        'url': {
            type: DataTypes.CHAR(255),
            allowNull: true
        },
        'arquivo': {
            type: DataTypes.CHAR(255),
            allowNull: true
        },
        'arquivo_topo': {
            type: DataTypes.CHAR(255),
            allowNull: true
        },
        'arquivo_bg': {
            type: DataTypes.CHAR(255),
            allowNull: true
        },
        'ordem': {
            type: DataTypes.INTEGER(11),
            allowNull: true
        },
        'destaque':{
            type: DataTypes.TINYINT(1),
            allowNull: true
        },
        'libera': {
            type: DataTypes.TINYINT(1),
            allowNull: true
        }
    },{
        tableName: "dn_coluna",
        freezeTableName: true,
        timestamps: false
    });

    sequelizePaginate.paginate(dn_coluna);
    return dn_coluna;
};

this is my Controller这是我的控制器

const { dn_coluna } = require("../models");
const { cfg_user } = require("../models");

module.exports = {
    async index(req, res){
        const colunas = await dn_coluna.findAll({
            include: [{
                model: cfg_user,
                attributes: []
            }],
            attributes: ['id_cfg_user'],
        })
        .then(colunas => {
            return res.status(200).json(colunas);
        });
    },
};

Your answer lies in associations.你的答案在于关联。 You are not properly associating your tables together.您没有正确地将您的表关联在一起。 Here is a link to the documentation . 这是文档的链接 I am unsure of the type of relationship that cfg_user and dn_coluna have so I can't show you how to write the association but the below may help.我不确定cfg_userdn_coluna之间的关系类型,因此我无法向您展示如何编写关联,但以下内容可能会有所帮助。

Remove the below from dn_coluna.jsdn_coluna.js删除以下dn_coluna.js

'id_cfg_user': {
    type: DataTypes.INTEGER(10),
    allowNull: false
 }

Add in to dn_coluna.js (just above sequelizePaginate.paginate(dn_coluna); ):添加到dn_coluna.js (就在sequelizePaginate.paginate(dn_coluna);之上):

dn_coluna.associate = (models) => {
    dn_coluna.hasMany(models.cfg_user, {foreignKey: 'cfg_user_id'})
}

In cfg_user.js add (just above sequelizePaginate.paginate(cfg_user); ):cfg_user.js添加(就在sequelizePaginate.paginate(cfg_user);之上):

cfg_user.associate = (models) => {
    cfg_user.belongsTo(models.dn_coluna)
}

The above will create a one to many relationship between dn_coluna and cfg_user .以上将在dn_colunacfg_user之间创建一对多关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM