【问题标题】:SEQUELIZE: belongsTo called with something that's not a subclass of Sequelize.ModelSEQUELIZE:belongsTo 调用的东西不是 Sequelize.Model 的子类
【发布时间】:2020-09-21 23:42:50
【问题描述】:

我是 sequelize 的新手,我正在尝试创建两个表。用户和项目。我为每个名为 UserModel 和 ProjectModel 的模型定义了一个模型,当我尝试将项目与用户关联时,我遇到了这个错误:

belongsTo 调用的东西不是 Sequelize.Model

这些是我定义每个模型的文件:

user.js

module.exports = (sequelize, DataType) => {
    const UserModel = sequelize.define("user", {
        id: {
            type: DataType.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false,
        },
        email: {
            type: DataType.STRING,
            allowNull: false,
            isEmail: {
                msg: "The format of the e-mail is not correct"
            },
            validate: {
                notNull: {
                    msg: "E-mail cannot be empty"
                }
            }
        },
        name: {
            type: DataType.STRING,
            is: /^[a-zA-Z ]+$/i,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Name cannot be empty"
                }
            }
        },
        surname: {
            type: DataType.STRING,
            is: /^[a-zA-Z ]+$/i,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Surname cannot be empty"
                }
            }
        }
    });
    UserModel.associate = (models) => {
        UserModel.hasMany(models.ProjectModel, {
            foreignKey: "userID"
        })
    }

    return UserModel;    
};

project.js

module.exports = (sequelize, DataType) => {
    const ProjectModel =  sequelize.define("project", {
        id: {
            type: DataType.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false,
        },
        name: {
            type: DataType.STRING,
            is: /^[a-zA-Z ]+$/i,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Name cannot be empty"
                }
            }
        },
        body: {
            type: DataType.TEXT,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Body cannot be empty"
                }
            }
        },
        status: {
            type: DataType.ENUM("active", "inactive", "declined", "completed"),
            allowNull: false,
            validate: {
                notNull: {
                    msg: "Status cannot be empty"
                }
            }
        },
        userID: {
            type: DataType.INTEGER,
            allowNull: false,
            validate: {
                notNull: {
                    msg: "userID cannot be empty"
                }
            },
            references: {
                model: UserModel,
                key: "id"
            }            
        }
    }); 
    ProjectModel.associate = (models) => {
        ProjectModel.belongsTo(models.UserModel, {
            foreignKey: "userID"
        });
    }

    ProjectModel.belongsTo(UserModel, {
        foreignKey: "userID"
    });


    return ProjectModel;
}

我做错了什么?

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    最后,我找到了解决问题的方法。在这个关联中,例如:

    ProjectModel.associate = (models) => {
        ProjectModel.belongsTo(models.UserModel, {
            foreignKey: "userID"
        });
    }
    

    使用变量 models.UserModel 我引用了模型返回的变量。取而代之的是,您必须写下表的名称。所以,就我而言,正确的功能是:

    ProjectModel.associate = (models) => {
        ProjectModel.belongsTo(**models.users**, {
            foreignKey: "userID"
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 1970-01-01
      • 2018-04-20
      • 2019-05-21
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2020-12-20
      相关资源
      最近更新 更多