-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Double include when reloading an instance with scopes #11010
Comments
I think the original issue was all the way back in V3 (#4658). Hopefully someone will write a test for this since it keeps getting introduced. |
Any update on this? |
This has been around for awhile now and is really the only thing preventing us from upgrading to V5. Any update? |
Sorry, no updates for now. I wanted to refactor the way scopes work, I would surely pay attention to fix this as a side-effect, but that would take too much time that I really don't have at the moment. |
Suddenly I understood what you're saying here! This was already fixed once (in #5106), right? And appeared again. This is unfortunate but at least there is an easier way to start re-investigating :) |
Hi ! this bug has been tested and reproduced in 5.21.13 . Is there any fix planned ? |
For a workaround doing |
example to reproduce in sequelize 6.3.5 const Feature = sequelize.define()
const Vendor = sequelize.define()
Vendor.hasMany(Feature)
Vendor.scope('withFeatures', () => ({include:[Feature]})) const vendor = await models.Vendor.scope(['withFeatures']).findOne()
vendor.reload() // failed with "SequelizeDatabaseError: Not unique table/alias: 'Features'" digging in the code I could see that reload uses
I fixed it by adding Vendor.scope('withFeatures', () => ({include:[{model: Feature, as: 'Features'}]})) |
@juliocanares Your workaround with defining the "as"-property explicitly solved my problem. I don't like this solution because it's redundant. But it helped me, thanks! This is my code. I simply added
This seems to be a known bug(?) since 2015 or earlier. Is this really that hard to fix? 🤔 A simple solution would be a default-value for the "as"-property with the Modelname in plural, or not? |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Issue still actual for Sequelize v6 |
Hello, as said upper, this is still present in Sequelize v6.5 too After some investigation the issue is happening in sequelize/lib/model.js line 4070 (reload function) options = Utils.defaults({
where: this.where()
}, options, {
include: this._options.include || undefined // This include the previous include
});
const reloaded = await this.constructor.findOne(options); // This will trigger again the includes in the scopes as a workaround I made this in my reload call reload({ include: [] })
// This will override the include in _options and remove the duplicated call, should work even if it's not empty until it's not the same include than the one in the scope Would be good if this case could be resolved still ... scopes are useful |
What are you doing?
Trying to use a scope within an include. The example is contrived but in our codebase we actually reuse lots of named scopes. (This is a reopen of #10501 , as the bug appears to still exist on version 5.8.6.)
To Reproduce
What do you expect to happen?
The task instance should just reload.
What is actually happening?
I get an SQL error and a crash :
sqlite:
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: ambiguous column name: project->user.id
PostgreSQL :
Unhandled rejection SequelizeDatabaseError: table name "project->user" specified more than once
The offending SQL looks like this (sqlite)
Environment
Dialect:
Dialect library version:
"sqlite3": "^4.0.6"
,"pg": "^7.4.1"
Database version: XXX
Sequelize version:
5.8.6
Node Version:
v10.15.3
OS:
Ubuntu 19.04
Tested with latest release:
5.8.6
The text was updated successfully, but these errors were encountered: