Where on association + limit throws db error
See original GitHub issueI am experiencing the following issue. I am trying to execute the following query:
transaction.findAll({
where: {
'$tenant.privileges.user_pkey$': {
$eq: userPkey
}
},
include: [
{
model: this.account,
as: 'tenant',
attributes: [],
include: [
{
model: this.privilege,
as: 'privileges',
attributes: []
}
]
}
],
order: 'transactionPkey',
limit: 10
});
I was expecting that I would get the top 10 transactions which fit the where clause.
However, instead I get a db error:
missing FROM-clause entry for table "tenant.privileges"
The actual SQL produced is this:
SELECT “Transaction”.* FROM (SELECT “Transaction”.“transaction_pkey” AS “transactionPkey”, “Transaction”.“global_transaction_pkey” AS “globalTransactionPkey”, “Transaction”.“tenant_pkey” AS “tenantPkey”, “Transaction”.“tenant_name” AS “tenantName”, “Transaction”.“counterparty_pkey” AS “counterpartyPkey”, “Transaction”.“counterparty_name” AS “counterpartyName”, “Transaction”.“transaction_direction_pkey” AS “transactionDirectionPkey”, “Transaction”.“transaction_direction_name” AS “transactionDirectionName”, “Transaction”.“transaction_status_pkey” AS “transactionStatusPkey”, “Transaction”.“transaction_status_name” AS “transactionStatusName”, “Transaction”.“transaction_type_pkey” AS “transactionTypePkey”, “Transaction”.“transaction_type_name” AS “transactionTypeName”, “Transaction”.“transaction_timestamp” AS “transactionTimestamp”, “Transaction”.“gross_quantity” AS “grossQuantity”, “Transaction”.“tare_quantity” AS “tareQuantity”, “Transaction”.“seal_identifier” AS “sealIdentifier”, “Transaction”.“vehicle_identifier” AS “vehicleIdentifier”, “Transaction”.“note”, “Transaction”.“image_path” AS “imagePath” FROM “v_transaction” AS “Transaction” WHERE (“tenant.privileges”.“user_pkey” = 158) ORDER BY “Transaction”.“transaction_pkey” asc LIMIT ‘10’ OFFSET ‘0’) AS “Transaction” LEFT OUTER JOIN “v_account” AS “tenant” ON “Transaction”.“tenantPkey” = “tenant”.“account_pkey” LEFT OUTER JOIN “v_privilege” AS “tenant.privileges” ON “tenant”.“account_pkey” = “tenant.privileges”.“account_pkey” ORDER BY “Transaction”.“transactionPkey” asc;
I believe this error is occuring because the where clause is in the inner subquery while the joins are done in the outer query.
Is there any way to have it all get executed as one query as opposed to putting the joins in the outer query and all other stuff (where, limit etc.) in the inner one?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:11 (2 by maintainers)
I guess this is a known issue since, at least, Feb 11 2015. See #3095
UPD: you can use this workaround: http://stackoverflow.com/questions/26021965/sequelize-with-nodejs-cant-join-tables-with-limit#comment48821718_26026326
Any news about this bug? option.subQuery=false helped me, but what about a fix in the core? Error:
sqlMessage: 'Unknown column \'odds.type_id\' in \'on clause\'',
Code:SQL: