gpt4 book ai didi

node.js - 尝试关联条目时,Sequelize 多对多失败并显示 'is not associated to'?

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:35 25 4
gpt4 key购买 nike

我的 Sequelize 多对多配置有问题,它提示 site_article_keyword 未与article_keyword 关联。下面的代码代表一个最小的测试用例,试图理解我做错了什么(我希望提供更小的东西,但这就是我所拥有的)。我正在使用 bluebird 作为 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() 的行上:

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 文档中的示例。

有人可以建议我做错了什么吗?

最佳答案

问题证明,site_article_keyword没有关联的原因是因为它是关联!考虑到这一点,代码将变为:

return ArticleKeyword.findAll({
where: {
language: language,
},
include: [{
model: SiteArticle,
as: 'SiteArticle',
siteArticleId: siteArticleId
}]
});

顺便说一句,对我的代码进行的一个小调整是将“as”包含到belongsToMany中:

ArticleKeyword.belongsToMany(
SiteArticle,
{ through: SiteArticleKeyword, as: 'SiteArticle' }
);

SiteArticle.belongsToMany(
ArticleKeyword,
{ through: SiteArticleKeyword, as: 'ArticleKeyword' }
);

这允许使用 addArticleKeyword() 而不是 addArticle_Keyword()

关于node.js - 尝试关联条目时,Sequelize 多对多失败并显示 'is not associated to'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42077987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com