问题描述
我在使用 Sequelize 的多对多配置时遇到问题,它抱怨 site_article_keyword 与 article_keyword
没有关联.下面的代码代表一个最小的测试用例,试图理解我做错了什么(我希望提供更小的东西,但这就是我所拥有的).我将 bluebird 用于 Promise API.
I am having a problem with my many to many configuration with Sequelize, where it complains that site_article_keyword is not associated to article_keyword
. The code below represents a minimal test case to try to understand what I am doing wrong (I hoped to provide something smaller, but this is what I have). I am using bluebird for the Promise API.
const Sequelize = require('sequelize');
var sequelize = new Sequelize(undefined, undefined, undefined, {
dialect: 'sqlite',
storage: './mydatabase',
});
const SiteArticle = sequelize.define('site_article', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
ownerId: {
type: Sequelize.INTEGER,
field: 'owner_id'
},
title: Sequelize.STRING
// other fields omitted
}, {
timestamps: true
});
const ArticleKeyword = sequelize.define('article_keyword', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: Sequelize.STRING,
language: Sequelize.STRING
// other fields omitted
}, {
timestamps: true
});
const SiteArticleKeyword = sequelize.define('site_article_keyword', {
siteArticleId: {
type: Sequelize.INTEGER,
field: 'site_article_id',
references: {
model: SiteArticle,
key: 'id'
}
},
articleKeywordId: {
type: Sequelize.INTEGER,
field: 'article_keyword_id',
references: {
model: ArticleKeyword,
key: 'id'
}
}
// other fields omitted
}, {
timestamps: true
});
(ArticleKeyword).belongsToMany(
SiteArticle, { through: SiteArticleKeyword });
(SiteArticle).belongsToMany(
ArticleKeyword, { through: SiteArticleKeyword });
这是定义的模型,现在用于尝试创建源条目和目标条目,然后我想要关联它们.失败发生在我调用 ArticleKeyword.findAll()
的行:
That's the model defined, now for trying to create the source and destination entries, that I then want to associate. The failure happens on the line where I call ArticleKeyword.findAll()
:
sequelize.sync({}).then(function() {
// populate some data here
let siteArticle;
SiteArticle.create({
ownerId: 1,
title: 'hello world'
}).then(function(entry) {
siteArticle = entry;
console.log('site article: ', JSON.stringify(entry, undefined, 2));
return ArticleKeyword.findOrCreate({
where: {
name: 'keyword1',
language: 'en'
}
});
}).spread(function(entry, success) {
console.log('article keyword: ', JSON.stringify(entry, undefined, 2));
return siteArticle.addArticle_keyword(entry);
}).spread(function(entry, success) {
console.log('site article keyword: ', JSON.stringify(entry, undefined, 2));
const siteArticleId = 1;
const language = 'en';
return ArticleKeyword.findAll({
where: {
language: language,
},
include: [{
model: SiteArticleKeyword,
where: {
siteArticleId: siteArticleId
}
}]
});
}).then(function(articleKeywords) {
if (articleKeywords) {
console.log('entries: ', JSON.stringify(articleKeywords, undefined, 2));
} else {
console.log('entries: ', 'none');
}
}).catch(function(error) {
console.log('ERROR: ', error);
}.bind(this));
}).catch(function(error) {
console.log(error);
});
我的代码基于 'Mixin BelongsToMany' 示例在 Sequelize 文档中.
I am basing my code on the 'Mixin BelongsToMany' example in the Sequelize documentation.
谁能建议我做错了什么?
Can anyone suggest what I am doing wrong?
推荐答案
在通过关系中,以下应该可以工作
In through relationships the following should work
ArticleKeyword.findAll({
include: [{
model: SiteArticle,
through: {
attributes: ['createdAt', 'startedAt', 'finishedAt'],
where: {siteArticleId: siteArticleId
}
}]
});
这篇关于Sequelize Many to Many 在尝试关联条目时以“未关联到"失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!