Repository navigation
Description
What are you doing?
I am testing with a minimal example whether or not scopes' includes are shallowly merged, as the documentation claims: limit, offset, order, paranoid, lock and raw are overwritten, while where and include are shallowly merged. (source: http://docs.sequelizejs.com/manual/tutorial/scopes.html)
const Sequelize = require("sequelize");
const sequelize = new Sequelize({ dialect: "sqlite", storage: "db.sqlite" });
const Foo = sequelize.define("foo", { name: Sequelize.STRING }, { timestamps: false });
const Bar = sequelize.define("bar", { name: Sequelize.STRING }, { timestamps: false });
Foo.hasMany(Bar, { foreignKey: "fooId" });
Foo.addScope("testScope", {
include: [{
model: Bar,
where: {
name: "bar1"
}
}]
});
sequelize.sync({ force: true })
.then(() => Foo.create({ name: "foo1" }))
.then(foo1 => Bar.create({ name: "bar1", fooId: foo1.id }))
.then(() => Foo.create({ name: "foo2" }))
.then(foo2 => Bar.create({ name: "bar2", fooId: foo2.id }))
.then(() => Foo.scope("testScope").findAll({
include: [{
model: Bar,
attributes: { exclude: ["name"] }
}]
}))
.then(result => console.log(JSON.stringify(result, null, 4)));What do you expect to happen?
I expected the scope to be merged with the findAll options, which should yield the following output:
[
{
"id": 1,
"name": "foo1",
"bars": [
{
"id": 1,
"fooId": 1
}
]
}
]What is actually happening?
The output was:
[
{
"id": 1,
"name": "foo1",
"bars": [
{
"id": 1,
"fooId": 1
}
]
},
{
"id": 2,
"name": "foo2",
"bars": [
{
"id": 2,
"fooId": 2
}
]
}
]Clearly, the scope was completely overwritten by the findAll options.
I wanted my code to behave exactly as if I had called the following:
Foo.scope("testScope").findAll({
include: [{
model: Bar,
where: {
name: "bar1"
},
attributes: { exclude: ["name"] }
}]
})Dialect: sqlite (sqlite3 v3.1.13)
Dialect version: 3.1.13
Sequelize version: 4.33.4
Tested with latest release: Yes
Note: if somehow this is the correct behavior, honestly the docs should be clarified. The include field is an array of objects, and "shallowly merging" an array of objects is not perfectly clear, does it mean that the arrays are concatenated? I hope not. I understand that the behavior I'm expecting is not trivial to code, but if the documentation claims it, it should do it. My understanding of "shallowly merging" in this context would be that the objects in the include arrays are "matched" to each other based on the "model-as" pairs, shallowly merging the other fields (and hopefully, recursively, since I could have nested includes in my scopes).
Thank you very much in advance, sequelize has been very useful so far 😁
Activity
yocontra commentedon Mar 29, 2018
@sushantdhiman Can you point me to where this behavior is supposed to happen? I can fix this and send a PR today.
This is important behavior to fix since people use these exclusion rules in scopes to hide password fields, api keys, etc. and specifying additional exclusion rules in a query will then override the scope and show these attributes. There are probably many people who are leaking sensitive attributes in the wild and don't even know it.
yocontra commentedon Mar 29, 2018
This might be a pretty easy fix - Line 2875 of model.js:
should be replaced with:
This will only allow future attributes options to pick from the output of the scope, which is the desired behavior. From my own tests this has fixed the issue for us.
sushantdhiman commentedon Mar 30, 2018
So I would like to clarify how scopes behave (as per my findings, they have been long part of sequelize before my involvement with project)
attributesandwhereoptions from all scopes combined under._scopedefaults over to what is passed asoptions.attributesandoptions.where. Consider this exampleoptions.includeof samemodel/associationpair will replace any sameincludefrom scopesfindAll.optionsas explained above.@contra #9252 looks invalid as per current behaviour which is to treat
scope.where/scope.attributesetc as defaults forfindAll.optionsyocontra commentedon Mar 30, 2018
@sushantdhiman Do you think that behavior is right? That sounds wrong to me IMO. I would expect that when I scope a model I won't be able to "escape" that scope no matter what options I pass to it later on. When I read the documentation previously I got the impression that this was how it was intended to work.
Could we discuss changing this behavior for v5? #9252 does some of it, would need a few additional changes to tackle
wheremerging if that isn't there already.papb commentedon Apr 3, 2018
Sorry to take so long to reply! Thanks @contra for taking the time to look into this, and thanks @sushantdhiman for the clarifications 😁. I'm looking forward to see this fixed, so I will do my best to reply faster.
Let me start with a few points:
1. I totally agree with @contra's last comment above. If the current behavior is the expected behavior, I think it should be changed.
2. In my opinion, the current docs for scopes does not reflect very well what was said in @sushantdhiman's clarifications above.
Whether or not the behavior will be changed, I think the docs deserve some improvements, but we can worry about improving the docs later (after we agree on the behavior itself).
3. @sushantdhiman - you said in your third bullet above that scopes are shallowly merged among themselves but not merged with
findAll.options. The docs seem to imply otherwise, where it says thatHere the deleted scope is merged with the finder. Regardless, if we make a minor change in my snippet from the first post, moving the options from thefindAllinto another new scope,testScope2, and callingFoo.scope("testScope", "testScope2").findAll()instead, we will get the same result anyway (and I still stand for my opinion that such behavior is wrong).papb commentedon Apr 4, 2018
Another very important clarification that I'd like to make is: when opening the issue, I tried to make a minimal example to convey my problem, and for that I arbitrarily chose to use the
attributesoption, but it could easily have been anything else, like alimitoption, or a nested include. To expand on this, hoping to clarify my goal when I opened the issue, I prepared another example:Take the following code:
Situation A results in:
Situation B results in:
Situation C results in:
As you can see, all results were different. I don't know if this is the expected behavior or not, but regardless, I think this is bad and counter-intuitive. Instead, I think Situations B and C should behave exactly as Situation A.
@contra and @sushantdhiman, do you agree with me?
Thanks again for taking the time!! 😃
sushantdhiman commentedon Apr 19, 2018
Sorry I haven't got back to this issue, busy with v5 milestone.
There is only one important change from milestone remaining after that I will propose something to tackle this issue. I intend to overhaul scopes in v5
papb commentedon Apr 19, 2018
@sushantdhiman Looks good, thanks for the update and for all the good work with Sequelize :)
When you do get back on this, I will be happy to discuss it further with you/others.
papb commentedon May 27, 2018
@sushantdhiman Hello again :) Have you considered adding this to the v5 milestone? Since you said you intend to overhaul scopes in v5, I think it would be a good idea to have this issue in the list.
The change I am proposing here is probably a breaking change, and this way it would be best if included in the v5 milestone (otherwise it would have to wait for v6?)
sushantdhiman commentedon Aug 31, 2018
Can you guys take a look at #9735 , essentially this PR is trying to fix #6245 but now we have came to a point that we have to change merge strategy for scopes.
If you can share your ideas in that PR we may be able to cover changes we discussed here in that PR
sushantdhiman commentedon Oct 28, 2018
@u9r52sld tackled some issues with scopes in #9735, now AFAICT we just need to decide merge strategy for scopes with finder options.
Can you guys reiterate your suggestions on how scopes should merge with finder options. What should get more priority i.e. attributes, includes etc from scopes or finder options
papb commentedon Oct 28, 2018
Hello @sushantdhiman. In my point of view, the options passed to
findAll()andfindOne()should merge exactly as if they were another scope.But the changes made by #9735 already address this - I've just retested to be sure, and everything I asked in this issue is indeed covered by #9735.
To me, there is nothing else to be done, you can close this issue if you want 😁
Let me know if you want further clarfications.
sushantdhiman commentedon Oct 29, 2018
Ah yes it does https://github.com/sequelize/sequelize/pull/9735/files#diff-a140b60b6a99a8c69f4b5d0d1f1f2bfaR3083 great. Will close this issue.
refactor(scope): documentation #9087 (#10312)
Upgrade sequelize to v5.1.0
Improve security for queue settings (#233)