续集协会不起作用

时间:2017-06-27 15:14:36

标签: node.js sequelize.js

我有两个模型发布和评论。帖子可以有很多评论,评论可以属于一个帖子。这是使用sequelize生成的类。

发表

'use strict';
module.exports = function(sequelize, DataTypes) {
  var post = sequelize.define('post', {
    title: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        post.hasMany(models.comment)
      }
    }
  });
  return post;
};

注释

'use strict';
module.exports = function(sequelize, DataTypes) {
  var comment = sequelize.define('comment', {
    title: DataTypes.STRING,
    body: DataTypes.STRING,
    postId: DataTypes.INTEGER
  }, {
    classMethods: {
      associate: function(models) {
        comment.belongsTo(models.post)
      }
    }
  });
  return comment;
};

以下是评论迁移文件:

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('comments', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      title: {
        type: Sequelize.STRING
      },
      body: {
        type: Sequelize.STRING
      },
      postId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: "posts",
          key:"id"
        }

      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('comments');
  }
};

我没有从帖子到评论的关系。

之后我创建一个帖子,然后创建一个评论。稍后,当我收到错误时,我会检索评论并指示获取与评论相关联的帖子:

Unhandled rejection SequelizeEagerLoadingError: post is not associated to comment!

以下是我使用的代码:

// create a post object
/*
const post = models.post.build({
    title: "Hello World"
})

// save a new post
post.save().then(function(newPost){
  console.log(newPost)
})
*/

// create a comment
/*
const comment = models.comment.build({
   title: "Comment",
   body: "Body",
   postId : 1
})

comment.save().then(function(newComment){
  console.log(newComment)
}) */

// get the comment and also the post it belongs to
models.comment.findOne({
  include: [
    {
      model: models.post 
    }
  ]
}).then(function(comment){
    console.log(comment)
})

2 个答案:

答案 0 :(得分:1)

首先,您需要处理拒绝,以防止未处理的拒绝错误:

models.comment.findOne({
  include: [
    {
      model: models.post
    }
  ]
}).then(function (comment) {
  console.log(comment)
}).catch(function (error) {
  console.log(error);
});

关于错误说明"post is not associated to comment"。看起来你错了实例。也许你需要发表评论而不是整篇文章:

include: [
  {
    model: models.post.comment
  }
]

答案 1 :(得分:0)

根据您创建的关联, 帖子与评论相关联,而不是相反。

所以你可以做到

                                         ; rax       eax          rdx       edx
; q = p++                                ; +----+----+----+----+  +----+----+----+----+
A1      mov     rax, QWORD PTR [rbp-8]   ; |                 p |  |               ??? |
A2      lea     rdx, [rax+4]             ; |                 p |  |               p+4 |
A3      mov     QWORD PTR [rbp-8], rdx   ; |                 p |  |               p+4 |
A4      mov     QWORD PTR [rbp-24], rax  ; |                 p |  |               p+4 |
; c = a++                                ; |                 p |  |               p+4 |
B1      mov     eax, DWORD PTR [rbp-40]  ; |       0 |       a |  |               p+4 |
B2      lea     edx, [rax+1]             ; |       0 |       a |  |       0 |     a+1 |
B3      mov     DWORD PTR [rbp-40], edx  ; |       0 |       a |  |       0 |     a+1 |
B4      mov     DWORD PTR [rbp-28], eax  ; |       0 |       a |  |       0 |     a+1 |
                                         ; +----+----+----+----+  +----+----+----+----+

但不是相反。 我建议你更多地考虑关联here

目前,您在帖子与评论之间的关系为1:m。你需要一个n:m关联来反过来。