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.

JSON format for queries using "include"

See original GitHub issue

First, thanks for the awesome library. Saves me a ton of time in my projects. I’ve had this problem for quite a while and made a bunch of workarounds, finally decided to just ask you guys. Thanks in advance!

Basically i want to query the db and include an association as nest json. EG:

User.findOne({
    raw: true,
    include: [{
      model: Company,
      as: 'company'
    }]
  })

What im getting is:

{
  id: 1234,
  name: 'Blah',
  company.id: 1234,
  company.name: 'blah blah'
}

I would expect:

{
  id: 1234,
  name: 'Blah',
  company: { 
    id: 1234,
    name: 'blah blah'
  }
}

What am i doing wrong here?

Dialect: postgres Database version: 9.5.2.0 Sequelize version: 3.23.4

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rafaelvmedeiroscommented, Apr 13, 2020

another option is set: nest: true

await Empresa.findOne({ where: {}, raw: true, nest: true, attributes: [‘cnpj’, ‘razao_social’, ‘inscricao_estadual’], include: [{}]

0reactions
felixfbeckercommented, Sep 9, 2016

Oh, and if it wasn’t clear, of course you can also just res.json() the result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Format Query Results as JSON with FOR JSON - SQL Server
In AUTO mode, the structure of the SELECT statement determines the format of the JSON output. By default, null values aren't included in...
Read more >
The JSON_QUERY() function to extract objects from JSON Data
In this article, we will explore JSON_QUERY() functions in SQL Server to extract JSON objects and array from the JSON Data.
Read more >
Simple Queries - JSONata Documentation
JSON arrays are used when an ordered collection of values is required. Each value in the array is associated with an index (position)...
Read more >
Basics of Working with JSON in SQL Server - {coding}Sight
The document in Listing 1 was extracted from a regular SQL Server database table using the query from Listing 2. Listing 2 shows...
Read more >
Query and Manipulate JSON Data with T-SQL - Pluralsight
The decision of whether including a root element or not depends on the needs of the application consuming the result set. In any...
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