最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin17浏览0评论

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

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

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

引发新的错误(${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model);^

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

const Sequelize = require('sequelize');

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

    }, {timestamp: false});

    return Post;
};

我的邮箱模型

const Sequelize = require('sequelize');

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

module.exports = function (sequelize) {
    var PostBox = sequelize.define('user_posts_boxes', {
    }, {timestamp: false});

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

研究期间,我从以下链接中找到了帮助hasMany called with something that's not an instance of Sequelize.Model

建议我使用全部方法和参考链接

回答如下:

您尚未在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;
};
发布评论

评论列表(0)

  1. 暂无评论