Repository navigation
Closed
Description
What are you doing?
These are the query parameters that I am using with findAll. If I use attributes and include at the same time, the query is completely invalid for various reasons:
var queryParameters = {
attributes: [
[ 'id', 'id' ],
[ 'account_id', 'accountId' ],
[ 'asset_id', 'assetId' ],
[ 'name', 'name' ],
[ 'thumbnail_name', 'thumbnailName' ],
[ 'enabled', 'enabled' ],
[ 'createdby', 'createdby' ],
[ 'createdate', 'createdate' ],
[ 'updateby', 'updateby' ],
[ 'updatedate', 'updatedate' ],
[ 'content_type', 'contentType' ],
[ 'content_value', 'contentValue' ] ],
include:
[ { model: asset_master, as: 'AssetMaster' },
{ model: account_asset_folder, as: 'AccountAssetFolders', through: { attributes: [] } } ],
where: { account_id: '100' },
order: [ [ 'name', 'asc' ] ],
limit: 100,
offset: 0 };What do you expect to happen?
If I remove the attributes property in my query parameters, it generates a functional query:
SELECT
`account_asset`.*,
`AssetMaster`.`id` AS `AssetMaster.id`,
`AssetMaster`.`name` AS `AssetMaster.name`,
`AssetMaster`.`md5sum` AS `AssetMaster.md5sum`,
`AssetMaster`.`width` AS `AssetMaster.width`,
`AssetMaster`.`height` AS `AssetMaster.height`,
`AssetMaster`.`size` AS `AssetMaster.size`,
`AssetMaster`.`dpi` AS `AssetMaster.dpi`,
`AssetMaster`.`update_date` AS `AssetMaster.update_date`,
`AssetMaster`.`update_by` AS `AssetMaster.update_by`,
`AssetMaster`.`create_date` AS `AssetMaster.create_date`,
`AssetMaster`.`create_by` AS `AssetMaster.create_by`,
`AssetMaster`.`enabled` AS `AssetMaster.enabled`,
`AssetMaster`.`asset_type_id` AS `AssetMaster.asset_type_id`,
`AccountAssetFolders`.`id` AS `AccountAssetFolders.id`,
`AccountAssetFolders`.`account_id` AS `AccountAssetFolders.account_id`,
`AccountAssetFolders`.`name` AS `AccountAssetFolders.name`,
`AccountAssetFolders`.`enabled` AS `AccountAssetFolders.enabled`,
`AccountAssetFolders`.`parent_folder_id` AS `AccountAssetFolders.parent_folder_id`,
`AccountAssetFolders->account_asset_folder_assignment`.`id` AS `AccountAssetFolders.account_asset_folder_assignment.id`,
`AccountAssetFolders->account_asset_folder_assignment`.`account_asset_id` AS `AccountAssetFolders.account_asset_folder_assignment.account_asset_id`,
`AccountAssetFolders->account_asset_folder_assignment`.`account_asset_folder_id` AS `AccountAssetFolders.account_asset_folder_assignment.account_asset_folder_id`
FROM
(SELECT
`account_asset`.`id`,
`account_asset`.`account_id`,
`account_asset`.`asset_id`,
`account_asset`.`name`,
`account_asset`.`thumbnail_name`,
`account_asset`.`enabled`,
`account_asset`.`createdby`,
`account_asset`.`createdate`,
`account_asset`.`updateby`,
`account_asset`.`updatedate`,
`account_asset`.`content_type`,
`account_asset`.`content_value`
FROM
`account_asset` AS `account_asset`
WHERE
`account_asset`.`account_id` = '100'
ORDER BY `account_asset`.`name` ASC
LIMIT 0 , 100) AS `account_asset`
LEFT OUTER JOIN
`asset_master` AS `AssetMaster` ON `account_asset`.`asset_id` = `AssetMaster`.`id`
LEFT OUTER JOIN
(`account_asset_folder_assignment` AS `AccountAssetFolders->account_asset_folder_assignment`
INNER JOIN `account_asset_folder` AS `AccountAssetFolders` ON `AccountAssetFolders`.`id` = `AccountAssetFolders->account_asset_folder_assignment`.`account_asset_folder_id`) ON `account_asset`.`id` = `AccountAssetFolders->account_asset_folder_assignment`.`account_asset_id`
ORDER BY `account_asset`.`name` ASC;What is actually happening?
This is the result of the query with an attributes object:
SELECT
`account_asset`.*,
`AssetMaster`.`id` AS `AssetMaster.id`,
`AssetMaster`.`name` AS `AssetMaster.name`,
`AssetMaster`.`md5sum` AS `AssetMaster.md5sum`,
`AssetMaster`.`width` AS `AssetMaster.width`,
`AssetMaster`.`height` AS `AssetMaster.height`,
`AssetMaster`.`size` AS `AssetMaster.size`,
`AssetMaster`.`dpi` AS `AssetMaster.dpi`,
`AssetMaster`.`update_date` AS `AssetMaster.update_date`,
`AssetMaster`.`update_by` AS `AssetMaster.update_by`,
`AssetMaster`.`create_date` AS `AssetMaster.create_date`,
`AssetMaster`.`create_by` AS `AssetMaster.create_by`,
`AssetMaster`.`enabled` AS `AssetMaster.enabled`,
`AssetMaster`.`asset_type_id` AS `AssetMaster.asset_type_id`,
`AccountAssetFolders`.`id` AS `AccountAssetFolders.id`,
`AccountAssetFolders`.`account_id` AS `AccountAssetFolders.account_id`,
`AccountAssetFolders`.`name` AS `AccountAssetFolders.name`,
`AccountAssetFolders`.`enabled` AS `AccountAssetFolders.enabled`,
`AccountAssetFolders`.`parent_folder_id` AS `AccountAssetFolders.parent_folder_id`,
`AccountAssetFolders->account_asset_folder_assignment`.`id` AS `AccountAssetFolders.account_asset_folder_assignment.id`,
`AccountAssetFolders->account_asset_folder_assignment`.`account_asset_id` AS `AccountAssetFolders.account_asset_folder_assignment.account_asset_id`,
`AccountAssetFolders->account_asset_folder_assignment`.`account_asset_folder_id` AS `AccountAssetFolders.account_asset_folder_assignment.account_asset_folder_id`
FROM
(SELECT
`account_asset`.`id`,
`account_asset`.`id` AS `id`,
`account_asset`.`name` AS `name`,
`account_asset`.`accountId` AS `accountId`,
`account_asset`.`asset_id` AS `assetId`,
`account_asset`.`thumbnail_name` AS `thumbnailName`,
`account_asset`.`enabled` AS `enabled`,
`account_asset`.`createdby` AS `createdby`,
`account_asset`.`createdate` AS `createdate`,
`account_asset`.`updateby` AS `updateby`,
`account_asset`.`updatedate` AS `updatedate`,
`account_asset`.`content_type` AS `contentType`,
`account_asset`.`content_value` AS `contentValue`
FROM
`account_asset` AS `account_asset`
WHERE
`account_asset`.`account_id` = '100'
ORDER BY `account_asset`.`name` ASC
LIMIT 0 , 100) AS `account_asset`
LEFT OUTER JOIN
`asset_master` AS `AssetMaster` ON `account_asset`.`asset_id` = `AssetMaster`.`id`
LEFT OUTER JOIN
(`account_asset_folder_assignment` AS `AccountAssetFolders->account_asset_folder_assignment`
INNER JOIN `account_asset_folder` AS `AccountAssetFolders`
ON `AccountAssetFolders`.`id` = `AccountAssetFolders->account_asset_folder_assignment`.`account_asset_folder_id`)
ON `account_asset`.`id` = `AccountAssetFolders->account_asset_folder_assignment`.`account_asset_id`
ORDER BY `account_asset`.`name` ASC;Which comes with this error. If I query for any of my ID columns, id, account_id, or asset_id with a corresponding join for any of the columns, the attributes property corrupts the query.
nhandled rejection SequelizeDatabaseError: Unknown column 'AccountAsset.asset_id' in 'on clause'
at Query.formatError (/Library/WebServer/adstudio/node_modules/sequelize/lib/dialects/mysql/query.js:228:16)
at Query.connection.query [as onResult] (/Library/WebServer/adstudio/node_modules/sequelize/lib/dialects/mysql/query.js:55:23)
at Query.Command.execute (/Library/WebServer/adstudio/node_modules/mysql2/lib/commands/command.js:30:12)
at Connection.handlePacket (/Library/WebServer/adstudio/node_modules/mysql2/lib/connection.js:515:28)
at PacketParser.onPacket (/Library/WebServer/adstudio/node_modules/mysql2/lib/connection.js:94:16)
at PacketParser.executeStart (/Library/WebServer/adstudio/node_modules/mysql2/lib/packet_parser.js:77:14)
at Socket.<anonymous> (/Library/WebServer/adstudio/node_modules/mysql2/lib/connection.js:102:29)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:563:20)Dialect: mysql
Dialect version: 5.6.35
Database version: 14.14
Sequelize version: 4.22.5
Tested with master branch: No
Note : Your issue may be ignored by maintainers if it's not tested against master branch OR does not follow issue template.
Metadata
Metadata
Assignees
Labels
No labels
Activity
mattdiamond commentedon Nov 16, 2017
This is somewhat related to my bug: #8612
The problem is that when Sequelize uses the subquery strategy, it populates the subquery select using the query attributes without checking to ensure that all necessary foreign keys are included as well.
wolframkleis commentedon Nov 22, 2017
I have the same problem. It seems to happen only if several things come together
you use "limit" - otherwise no subquery is used
you include one "belongsTo" association, and a second one of type "toMany" or "hasMany" -otherwise no subquery is used
attributes are specified and the "belongsTo" foreign key is not in the attributes array - otherwise the subquery would select the foreign key
mattdiamond commentedon Nov 22, 2017
@wolframkleis Yeah, it's definitely a bit of an edge case. I should note that the query will work with an attributes hash as long as the
belongsToassociation has already been declared. The call toModel.belongsTowill add the foreign key to the model'srawAttributesobject, which is used to expand the attributes for a given query. If the association hasn't been declared yet (for example, if the query is using a scope that was declared before the association) then this bug can occur.Hopefully they'll take a look at this... I haven't gotten a response to my bug report yet.
wolframkleis commentedon Nov 23, 2017
It happens even if you declare the foreign key attribute from the beginning with
sequelize.define()Example:
Error:
error: column Book.authorId does not existSQL:
The sub query does not select the
authorId, and the 2nd JOIN is broken.NobleUplift commentedon Dec 7, 2017
@wolframkleis helped me find yet another bug in Sequelize, which is evident based on query above. If I have
LIMIT 0 , 100in the subquery and it loads the first 100 rows, any rows past that that belong to the joined table are excluded from the count. I have it where any new objects created are never seen, because they all have IDs in the 1000s, even though there are only 5 items in the folder. Removing the limit/offset so that it can get the 5 items after the first couple 1000 results in:How do I apply
limitandoffsetto my parent query, and not my subquery? Does this constitute a new bug?stale commentedon Mar 8, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
wolframkleis commentedon Mar 8, 2018
Not stale. Error is still there, just tested ist with 4.35.2
sushantdhiman commentedon Apr 7, 2018
Moved to #9166