Sequelize.Model关系在NodeJS中不起作用

时间:2019-09-26 08:00:18

标签: node.js sequelize.js

你好,我是NodeJs的新用户,我在自己的NodeJs项目中集成了Sequelize库并定义了我的模型。当我添加模型的关系时,会出现以下错误

  
    

引发新错误(schema_migrations);           ^

  
     

错误:user_posts_boxes.belongsTo用非   Sequelize.Model的子类       在功能。 (/home/rizwan/php/nodejs/node_modules/sequelize/lib/associations/mixin.js:93:13)   我的帖子模型

${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model

我的PostBox模型

const Sequelize = require('sequelize');

module.exports = function (sequelize) {
    var Post = sequelize.define('user_posts', {

    }, {timestamp: false});

    return Post;
};

在研究过程中,我从以下链接获得了帮助 hasMany called with something that's not an instance of Sequelize.Model

建议我使用完整的引用和参考链接

1 个答案:

答案 0 :(得分:0)

您尚未在PostBox模型中定义任何列。 尝试:

const Sequelize = require('sequelize');

var Post = require('./Post');

module.exports = function (sequelize) {
    var PostBox = sequelize.define('user_posts_boxes', 
    {
      post_id: {
        type: Sequelize.INTEGER,
        references: {
            model: Post,
            key: "post_id" // this is the `posts.post_id` column - name it according to your posts table definition (the one defined in ./Post)
        }
      }
    }, {timestamp: false});

    PostBox.belongsTo(Post, {foreignKey: 'post_id'});
    return PostBox;
};