question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement "flattening" when including fields in an association table

See original GitHub issue

I am not sure how to title this issue, but let me explain.

Suppose we have two tables Person and Project plus a Role association connecting a person with one or more projects (in both directions). The Role association/table has a field called say RoleType.

I could set this up and query the db with for example:

Person.find({
   where: { id: 1 },
   include: [
      { model: Project, as: "projects", attributes: ["id", "name"], though: { attributes: "roleType" } }
   ]
})

which could produce something like

{
 name: "John Smith",
 id: 1,
 project: [ { id: 'a', name: 'Fix issue', role: { roleType: "manager" } }]
}

My issue is about the sub object role: { roleType: "manager" }. What I would like is a return similar to this:

{
 name: "John Smith",
 id: 1,
 project: [ { id: 'a', name: 'Fix issue', roleType: "manager" } ]
}

Namely I would like the roleType field to be available directly on each project item.

Is this possible to achieve? It would be nice to have it. Any suggestions for work arounds?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
evanslifycommented, Mar 23, 2017

Sorry to ‘resurrect’ this issue, but may I ask if this is implemented in sequelize now, as I couldn’t find it in documentation?

11reactions
mehemmelbachircommented, Feb 21, 2019

Yes, you can add option raw : true so it becomes like:

Person.find({
   where: { id: 1 },
   include: [
      { model: Project, as: "projects", attributes: ["id", "name"], though: { attributes: "roleType" } }
   ],
   raw : true
})

And you can have a result like:

id : 1
name : 'Jhon'
...
Project.id : 4
Project.name : 'project name'
....
Read more comments on GitHub >

github_iconTop Results From Across the Web

Flattening columns in sub associations with sequelize
I have solved by using sequelize literal, it's a little raw but it works but you must know all associations before the column....
Read more >
FLATTEN Data Using Google BigQuery's Legacy vs ...
This acts similarly to Entity SQL's FLATTEN function by purposefully flattening the specified field into the rest of the dataset.
Read more >
LWC Relationship fields in data table, flattening in @wire
I am struggling with relationship fields in a datatable using Lightning Web Components. I am querying through Apex and returning a list from ......
Read more >
Part#8. CDS Views – Joins and Associations
We saw that how a CDS view fetched data from 2 tables with the ... the business user is looking at only fields...
Read more >
Creating Flatten Views — Virtual DataPort Administration ...
If you need to “flatten” the fields of a register and not an array, do not create a Flatten view. Instead, create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found