Sequelize의 다 대다 구성에 문제가 site_article_keyword is not associated to article_keyword
있습니다. 아래 코드는 내가 뭘 잘못하고 있는지 이해하기위한 최소한의 테스트 케이스를 나타냅니다 (작은 것을 제공하고 싶었지만 이것이 내가 가진 것입니다). Promise API에 bluebird를 사용하고 있습니다.
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);
});
Sequelize 문서 의 ' Mixin OwnerToMany '예제를 기반으로 코드를 작성하고 있습니다.
누구든지 내가 뭘 잘못하고 있는지 제안 할 수 있습니까?
관계를 통해 다음이 작동해야합니다.
ArticleKeyword.findAll({
include: [{
model: SiteArticle,
through: {
attributes: ['createdAt', 'startedAt', 'finishedAt'],
where: {siteArticleId: siteArticleId
}
}]
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다