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.

Unable to order by associations conditionally

See original GitHub issue

I have a model, say Base which has one Details1 model or has one Details2 model depending on the value of a field “type” (which says either details1 or details2).

Both Details1 and Details2 have a field “orderField”.

I need a way to order rows of the Base model by either Details1.orderField or Details2.orderField depending on whether Base.type = details1 or details2

I have tried looking up the sequelize docs relating to ordering: http://docs.sequelizejs.com/manual/querying.html#ordering

Approach 1:

{
  include: [
     { model: Details1 },
     { model: Details2 }
  ],
  order: [
     [
       { model: Details1, as: 'Details1' }, 'orderField', 'DESC'
     ],[
       { model: Details2, as: 'Details2' }, 'orderField', 'DESC'
     ]
  ]
});

The above code however orders rows of Base models by Details1.orderField separately and by Details2.orderField separately.

For instance, if I had the following data:

{type: details1, Details1: {orderField: 2}}
{type: details2, Details2: {orderField: 1}}
{type: details2, Details2: {orderField: 5}}
{type: details1, Details1: {orderField: 4}}
{type: details2, Details2: {orderField: 3}}

Then the expected order is:

{type: details2, Details2: {orderField: 5}}
{type: details1, Details1: {orderField: 4}}
{type: details2, Details2: {orderField: 3}}
{type: details1, Details1: {orderField: 2}}
{type: details2, Details2: {orderField: 1}}

However, I am getting the output as:

{type: details1, Details1: {orderField: 4}}
{type: details1, Details1: {orderField: 2}}
{type: details2, Details2: {orderField: 5}}
{type: details2, Details2: {orderField: 3}}
{type: details2, Details2: {orderField: 1}}

Approach 2:

{
  include: [
     { model: Details1, as: 'Details1' },
     { model: Details2, as: 'Details2' }
  ],
  order: [
     [
        sequelize.literal('case when "Base".type = \'details1\' then "Details1".orderField else "Details2".orderField end'
        )
      ]
  ]
};

However, this fails with an error: missing FROM-clause entry for table “Details1”

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any Dialect library version: XXX Database version: XXX Sequelize version: 3.30.4 Node Version: 6.13.0 OS: XXX If TypeScript related: TypeScript version: XXX Tested with latest release:
  • No
  • Yes, specify that version:

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Jigar-Freightwallacommented, Sep 6, 2019

@papb Thank you for the prompt response.

0reactions
github-actions[bot]commented, Nov 30, 2021

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the “stale” label. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Sequelize - order by associations conditionally
One thing you could try: use a CASE statement to derive a single field for sorting. order: [ [Sequelize.literal("case when details1.
Read more >
Ordering To-Many Associations - ORM
To retrieve a sorted collection from the database you can use the #[OrderBy] attribute with a collection that specifies a DQL snippet that...
Read more >
Associations
Sequelize supports the standard associations: One-To-One, One-To-Many and ... The order in which the association is defined is relevant.
Read more >
How to Avoid Conditional JOINs in T-SQL - Simple Talk
Recently I was writing a query that could be resolved using the second case. While I thought about several other ways to do...
Read more >
Is it possible to set up conditional associations in Rails?
The condition filters the association to those records where "paid" evaluates to true. If you create a magazine under a user, Rails will...
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