這是我的代碼,我真的不知道如何解決這個問題了
當我輸入此代碼時。
db.models.Price.findAll({
attributes:['id', 'item.item_name'],
group: [
['masterfile_price.id','item.id','item.item_name']
],
// [sequelize.literal('Item.item_name')]
// ['masterfile_price.id']
raw: true,
include: [{
model: db.models.Item,
as: 'item',
attributes: ['id',"item_name"],
}],
})
它會給我這個錯誤錯誤DatabaseError [SequelizeDatabaseError]:列“item.item_name”必須出現在GROUP BY子句中或在聚合函式中使用我不知道為什么
查詢是這樣的
SELECT
"masterfile_price"."id",
"item"."item_name",
"item"."id" AS "item.id",
"item"."item_name" AS "item.item_name"
FROM
"masterfile_prices" AS "masterfile_price"
LEFT OUTER JOIN "masterfile_items" AS "item" ON "masterfile_price"."itemId" = "item"."id"
GROUP BY
"masterfile_price"."id";
問題是我只想要 item_name
在我的原始查詢中不使用 sequelize 它正在作業
SELECT
"item"."item_name" AS "item.item_name"
FROM
"masterfile_prices" AS "masterfile_price"
LEFT OUTER JOIN "masterfile_items" AS "item" ON "masterfile_price"."itemId" = "item"."id"
GROUP BY
item.item_name;
這將導致
| item.item_name |
|---|
| 測驗9 |
| 測驗10 |
我不知道為什么我不能在續集中復制它,或者我錯過了什么?
或者sequelize group by有什么問題嗎
uj5u.com熱心網友回復:
group選項應該是一個string | Fn | Col或一個陣列string | Fn | Col。所以你需要洗掉額外的括號:
group: ['masterfile_price.id','item.id','item.item_name']
如果您只想選擇和分組,item.item_name那么您只需要在attributes和group選項中指明它:
db.models.Price.findAll({
attributes:[[Sequelize.col('item.item_name'), 'item_name']],
group: Sequelize.col('item.item_name'),
raw: true,
include: [{
model: db.models.Item,
as: 'item',
attributes: [],
}],
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/424030.html
標籤:javascript 节点.js 续集.js
