[英]Cannot associate tables in sequalize (node js + sequalize)
I've got two tables that are structured like this: 我有两个结构如下的表:
FeedPost id, text, likecount, commentcount FeedPost ID,文本,likecount,commentcount
Comment id, text, lkpfeedpostid 评论ID,文字,lkpfeedpostid
So there's a foreign key from comment to feedpost via lkpfeedpostid. 因此,从注释到通过lkpfeedpostid的feedpost有一个外键。 So one FeedPost could have many Comment.
因此,一个FeedPost可能会有很多评论。 I'm trying to set this up in sequalize so that when I do a FeedPost.findAll(), it pulls back the feed posts with all the comments.
我试图将其设置为sequalize,以便当我执行FeedPost.findAll()时,它将拉回具有所有注释的提要帖子。
Here's how I've set it up: 这是我的设置方法:
Model Definitions: 型号定义:
FeedPost: FeedPost:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('FeedPost', {
...
...
}, {
tableName: 'FeedPost',
classMethods: {
associate : function(models) {
FeedPost.hasMany( Comment, { as: 'comments' });
},
},
});
};
Comment: 评论:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Comment', {
...
...
}, {
tableName: 'Comment',
classMethods: {
associate : function(models) {
Comment.belongsTo(FeedPost, { foreignKey: 'lkpfeedpostid'})
},
},
});
};
I'm importing them like: 我像这样导入它们:
const FeedPost = sequelize.import('../models/FeedPost');
const Comment = sequelize.import('../models/Comment');
And passing them into an express router like: 并将它们传递到快递路由器中,例如:
app.use(feed(FeedPost, Comment));
module.exports = (FeedPost, Comment) => {
const api = Router();
api.get('/api/feed', (req, res) => {
FeedPost.findAll( { include: [ {model: Comment, as: 'Comment' }]}).then(results => {
res.json({results});
});
}
I've tried tons of variations of this found in these links: 我尝试了在以下链接中找到的大量变化形式:
http://docs.sequelizejs.com/manual/tutorial/models-definition.html#import http://docs.sequelizejs.com/manual/tutorial/models-definition.html#import
http://docs.sequelizejs.com/class/lib/associations/has-many.js~HasMany.html http://docs.sequelizejs.com/class/lib/associations/has-many.js~HasMany.html
https://github.com/sequelize/sequelize/issues/3273 https://github.com/sequelize/sequelize/issues/3273
And whenever I hit the route, I keep getting the error that says 每当我走上路线时,我都会不断收到错误消息
Unhandled rejection SequelizeEagerLoadingError: Comment is not associated to Feed!
I am going with that assumption that your sequelize version >=4. 我假设您的续集版本> = 4。
classMethods
and instanceMethods
have been removed. classMethods
和instanceMethods
已被删除。 The changelog can be found here : Sequelize V4 Breaking Changes 可以在这里找到更改日志: Sequelize V4重大更改
From their docs you can refer the current method to specify associations. 从他们的文档中,您可以引用当前方法来指定关联。
FeedPost FeedPost
module.exports = function(sequelize, DataTypes) {
const FeedPost = sequelize.define('FeedPost', {/*Attributes here*/});
FeedPost.hasMany(Comment, { as: 'comments' })
};
Comment 评论
module.exports = function(sequelize, DataTypes) {
const Comment = sequelize.define('Comment', {/*Attributes here*/});
Comment.BelongsTo(Comment, { foreignKey: 'lkpfeedpostid', targetKey: 'id' })
};
The following query should work: 以下查询应该工作:
FeedPost.findAll( { include: [ {model: Comment, as: 'comments' }]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.