중첩된 내용이 후속 버전에 포함됩니까?
중첩 포함을 수행하는 방법?저는 댓글과 1대 다수의 연관성을 가진 테이블 제품을 가지고 있고, 테이블 댓글은 사용자 테이블과 1대 다수의 연관성을 가지고 있습니다.그래서 댓글에는 user_id와 product_id가 있습니다.내 코드는 이렇습니다.
var models = require('../models');
models.products.findAll({
include: [
{model: models.comments}
]
}).then(function (products) {
next(products);
}).catch(function (err) {
next(err);
});
});
댓글들은 이해하지만, 저는 이런 것을 갖고 싶습니다.
models.products.findAll({
include: [
{model: models.comments.include(models.comments.users)}
]
})
사용자 지정 쿼리를 작성하지 않아도 가능합니까?
models.products.findAll({
include: [
{model: models.comments, include: [models.comments.users] }
]
})
제공된 솔루션은 제게 효과가 없었습니다. 이것이 제가 사용하는 타이프스크립트 버전이고 후속 버전을 추측하는 방법입니다.
// sequelize-typescript
models.products.findAll({
where,
include: [{
model: Comment,
include: [User]
}]
});
// without typescript (guessing here)
models.products.findAll({
where,
include: [{
model: models.comments,
include: [{
model: models.users
}]
}]
});
아주 간단하고 간결한 방법이 있습니다! https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/ #포함-모든 것
// Fetch all models associated with User
User.findAll({ include: { all: true }});
// Fetch all models associated with User and their nested associations (recursively)
User.findAll({ include: { all: true, nested: true }});
미래에 누군가에게 도움이 될 경우:
여러 단계를 깊이 중첩하고 있었는데 호출이 오버라이드와 일치하지 않는 타이프스크립트 오류가 계속 발생했습니다.
저는 이전 답변(또는 문서)에서 다음을 포장해야 한다는 것이 명확하지 않았습니다.include모든 레벨에서 배열된 객체(심지어 당신이 1개의 객체만 제공함).
작업:
User.findAll({
include: [{
model: Contact,
include: [{
model: Address,
include: [{
model: City,
include: [State, Country]
}]
}]
}]
});
작동하지 않음:
User.findAll({
include: { // <-- notice the missing square bracket
model: Contact,
include: { // <-- notice the missing square bracket
model: Address,
include: { // <-- notice the missing square bracket
model: City,
include: [State, Country]
} // <-- notice the missing square bracket
} // <-- notice the missing square bracket
} // <-- notice the missing square bracket
});
사용가능include.
예를 들어 다음과 같습니다.
Category = sequelize.define(...);
Product = sequelize.define(...);
Product.belongsTo(Category, {foreignKey: 'fk_category'});
Product.findAll({
include: [{
model: Category
}]
});
ID 뿐만 아니라 중첩된 카테고리를 검색합니다.category소유물.
언급URL : https://stackoverflow.com/questions/33941943/nested-include-in-sequelize
'programing' 카테고리의 다른 글
| check_admin_referer()가 포함된 Wordpress 함수를 PHP unit에서 어떻게 테스트하시겠습니까? (0) | 2023.11.01 |
|---|---|
| Get Value of Radio 버튼 그룹 (0) | 2023.10.27 |
| ExtractValue()가 업데이트에서 실패하고 SELECT에 성공하는 이유는 무엇입니까? (0) | 2023.10.27 |
| mysql 데이터베이스에서 JSON 배열을 구축하는 방법 (0) | 2023.10.27 |
| Android에서 보기에 대한 불투명도(Alpha)를 설정하는 방법 (0) | 2023.10.27 |