在Sequelize模型中如何具有多个.belongsTo()?

时间:2018-12-18 19:46:55

标签: sequelize.js

我正在创建一个页面,允许用户发布页面,并对这些帖子发表评论。当我尝试将评论与用户和帖子相关联时,我得到了Unknown column 'Comments.UserId' in 'field list'。有办法解决吗?

module.exports = function (sequelize, DataTypes) {
    var Comment = sequelize.define("Comment", {
        user: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        comment: {
            type: DataTypes.TEXT,
            allowNull: false,
        }
    });

    Comment.associate = function (models) {
        // We're saying that a Comment should belong to a Post
        // A Post can't be created without a User due to the foreign key constraint
        Comment.belongsTo(models.Post, {
            foreignKey: {
                allowNull: false
            }
        });
        Comment.belongsTo(models.User, {
            foreignKey: {
                allowNull: false
            }
        });
    };

    return Comment;
};

0 个答案:

没有答案