Repository navigation
Fixed attribute:[] issues when using them in include or through - #6297
Conversation
|
|
|
Ah i guess it might have failed specifically for N:M |
|
|
|
Just want to say that this is awesome. Thanks so much! Stupid question - how do I know when a new version of Sequelize is released with these fixes? |
|
@markdboyd master is gunning for 4.0, we have a lot of stuff on the roadmap though so might be a while |
|
Got it. Thanks! |
|
@markdboyd I use Sibell. You can of course also backport this to the v3 branch, there have not been many changes to the codebase so far besides ES6 refactoring. |
|
Is it in the latest tag? 3.24.6 is not contain this commit. |
|
@dodderjs this has been merged into master, not v3. |
|
Will it be merged into v3 at anytime? :) |
|
If anyone takes the time to backport, then yes :) |
|
It seems this just "hides" the attributes but still gets them from the database. For my use case, running on But when I look at the SQL Query, it did query every attribute of the joint table. Is there any specific reason for that? In my use case the joint table is a bit heavy and I'm fetching quite a bit of data too, so querying one or two fields instead of 15 will have a high impact. |
|
@Esya at least the IDs are needed I think. But anything else of course is unneeded |
|
@Esya IIRC we at least need to query for Ids, rest we can ignore but haven'y tried, I guess required for populating the instances |
|
I'm going to try to explicitly put the column names of the two Ids, but in On Nov 15, 2016 1:42 AM, "Sushant" [email protected] wrote:
|
|
@felixfbecker @sushantdhiman I can confirm that I get the same behavior if I specify that I want the IDs in the attributes. Below is my scope, that should return the users, and each projects that the user is linked to, and for each User<->Project it should give me the startTime (when User started Project). It should omit all the other UserProject fields. db.User.addScope('list', {
attributes: ['id','name','firstname'],
include: [{
// My UserProject association
association: db.User.Projects,
attributes: ['id','title'],
through: {
attributes: ['userId','projectId','startTime'],
}
}]
});What I get when doing |
|
@Esya we need to build the instances for all included and parent model so we have to query for all attributes. We can't have instances with missing data. |
|
@sushantdhiman I don't understand - isn't that exactly what |
|
Then why does the documentation mention that it's possible to do so ?
Edit: If this means that under the hood, sequelize queries ALL attributes and then masks some for you, then it's deceptive and the doc should rather say "Build your own raw query for this" |
|
I went around the problem for my use case which is, defining the 3 schemas as usual, then : UserProject.belongsTo(User)
UserProject.belongsTo(Project)
User.hasMany(UserProject)
Project.hasMany(UserProject)Now if I do : User.addScope('list', {
attributes: ['id','name','firstname'],
include: [{
model: UserProject,
attributes: ['startTime'],
include: [{ model: Project, attributes: ['title']}]
}]
});Then it works and Sequelize builds a sane query. I don't know how 2 hasMany + 2 belongsTo like that works better than belongsToMany, but that's a good workaround for me. |
|
includeIgnoreAttributes: false, solves the problem in sequelize 4. |
This is probably a workaround if you want to omit all attributes (i.e. I have a use case where there are two user accounts to the same database with different privileges. The restricted account is not allowed to query all fields from the through-table, so querying will not work as I can't actually restrict the attribute querying. This would be ideal but will not work: Course.belongsToMany(models.Period, { through: models.CoursePeriod });
Course.findAll({ include: [
{
model: db.models.Period,
attributes: ['name'],
through: {
attributes: ['startdate', 'enddate']
}
]
}
]});My workaround is basically the same as @Esya showed #6297 (comment) except that I cannot refactor the Course.belongsToMany(models.Period, { through: models.CoursePeriod });
Course.hasMany(models.CoursePeriod, { as: 'CoursePeriod2' });
Course.findAll({ include: [
{
model: db.models.CoursePeriod,
as: 'CoursePeriod2',
attributes: ['startdate', 'enddate'],
include: [
{
model: db.models.Period,
attributes: ['name']
}
]
}
]});I'm using sequelize v. 4.27.34 |
Pull Request check-list
npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?Description of change
Using
through.attributes:[]orinclude.attributes: []doesn't ignore any attributes, It throws error in most cases. Fixed it by checking if parent include exists before trying to push child include in it.Closes #5078
Closes #4222
Closes #5958
Closes #5590
Closes #6139
Closes #4866
Closes #6242