【问题标题】:How to associated two models in node JS如何在节点 JS 中关联两个模型
【发布时间】:2019-05-23 05:00:45
【问题描述】:

我正在尝试在这两个表之间建立这种连接,但我没有得到它并且它给出了以下错误:“UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: cfg_user is not associated to dn_coluna!”

以下是我用于内部连接的控制器和模型

这是我的 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;
};

这是我的 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;
};

这是我的控制器

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

【问题讨论】:

    标签: node.js join sequelize.js


    【解决方案1】:

    您的答案在于联想。您没有将表格正确关联在一起。 Here is a link to the documentation。我不确定cfg_userdn_coluna 的关系类型,所以我无法向您展示如何编写关联,但以下内容可能会有所帮助。

    dn_coluna.js中删除以下内容

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

    添加到dn_coluna.js(就在sequelizePaginate.paginate(dn_coluna);上方):

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

    cfg_user.js 中添加(就在sequelizePaginate.paginate(cfg_user); 上方):

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

    以上将在dn_colunacfg_user 之间创建一对多关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多