Skip to content

Fixed attribute:[] issues when using them in include or through - #6297

Merged
mickhansen merged 1 commit into
masterfrom
fix-nested-attributes
Jul 18, 2016
Merged

mickhansen merged 1 commit into
masterfrom
fix-nested-attributes

Conversation

@sushantdhiman

Copy link
Copy Markdown
Contributor

Pull Request check-list

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does your issue contain a link to existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Have you added an entry under Future in the changelog?

Description of change

Using through.attributes:[] or include.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

@mickhansen

mickhansen commented Jul 18, 2016 •

Copy link
Copy Markdown
Contributor

include.attributes: [] has always worked fine for me, did the test you added fail before applying fixes?

@mickhansen

Copy link
Copy Markdown
Contributor

Ah i guess it might have failed specifically for N:M

@mickhansen
mickhansen merged commit 34abeaa into master Jul 18, 2016
@mickhansen
mickhansen deleted the fix-nested-attributes branch July 18, 2016 10:14
@sushantdhiman

Copy link
Copy Markdown
Contributor Author

N:M with ignoring through.attributes only worked before the fixes

@markdboyd

markdboyd commented Jul 18, 2016 •

Copy link
Copy Markdown

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?

@mickhansen

Copy link
Copy Markdown
Contributor

@markdboyd master is gunning for 4.0, we have a lot of stuff on the roadmap though so might be a while

@markdboyd

Copy link
Copy Markdown

Got it. Thanks!

@felixfbecker

Copy link
Copy Markdown
Contributor

@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.

@dodderjs

Copy link
Copy Markdown
Contributor

Is it in the latest tag? 3.24.6 is not contain this commit.

@felixfbecker

Copy link
Copy Markdown
Contributor

@dodderjs this has been merged into master, not v3.

@dodderjs

Copy link
Copy Markdown
Contributor

Will it be merged into v3 at anytime? :)

@janmeier

Copy link
Copy Markdown
Member

If anyone takes the time to backport, then yes :)

@Esya

Esya commented Nov 15, 2016

Copy link
Copy Markdown

It seems this just "hides" the attributes but still gets them from the database.

For my use case, running on master, I have "User", "UserProject" and "Project".
When I do a findAll on User, with include: Project and specify some attributes for the through relationship, in the end it appears to work, my object only has the attributes I asked for if I do User.toJSON().projects for example.

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.

@felixfbecker

Copy link
Copy Markdown
Contributor

@Esya at least the IDs are needed I think. But anything else of course is unneeded

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

@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

@Esya

Esya commented Nov 15, 2016

Copy link
Copy Markdown

I'm going to try to explicitly put the column names of the two Ids, but in
any case sequelize could and should figure out that the two keys are not
included and add those to the query, but not include the whole structure of
the join table, thoughts?

On Nov 15, 2016 1:42 AM, "Sushant" [email protected] wrote:

@Esya https://github.com/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

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#6297 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA5M20kvHnBnLaL-zmQmdPwYUfl196R5ks5q-X58gaJpZM4JOj-M
.

@Esya

Esya commented Nov 15, 2016 •

Copy link
Copy Markdown

@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 console.log(users[0].toJSON().projects) looks fine (I only have userId,projectId and startTime), but I do see that Sequelize sent to postgres the full list of fields of the UserProject table. Am I missing something here?

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

@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.

@felixfbecker

Copy link
Copy Markdown
Contributor

@sushantdhiman I don't understand - isn't that exactly what attributes is for, omitting data?

@Esya

Esya commented Nov 15, 2016 •

Copy link
Copy Markdown

Then why does the documentation mention that it's possible to do so ?

With Belongs-To-Many you can query based on through relation and select specific attributes

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"

@Esya

Esya commented Nov 15, 2016

Copy link
Copy Markdown

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.

@rodrigoclp

Copy link
Copy Markdown

includeIgnoreAttributes: false, solves the problem in sequelize 4.

@rhietala

Copy link
Copy Markdown

includeIgnoreAttributes: false, solves the problem in sequelize 4.

This is probably a workaround if you want to omit all attributes (i.e. through: { attributes: [] }), but will not solve the problem if you want to include only some attributes.

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 belongsToMany relation.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants