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.

Using a column from a join table in the primary WHERE clause

See original GitHub issue

Hi, all.

Is it possible to produce equivalent SQL to this with findOne and some combination of includes?

SELECT * FROM Stories
INNER JOIN Contributions ON (Stories.id = Contributions.StoryId)
INNER JOIN Members AS Contributors ON (Contributions.ContributorId = Contributors.id)
WHERE (Stories.status = 'DRAFT' AND (Stories.AuthorId = 1 OR Contributors.id = 1));

It’s not clear to me from the documentation, source, or my own experimentation. Basically, I need to find a Story given either an Author id or a Contributor id, and while I can certainly run a raw query, I will also need to eager load additional associated models (which is tidier with findOne).

This is my Story model:

const Story = sequelize.define('Story', {
…
}, {
  …
  classMethods: {
    associate: (models) => {
      …
      Story.hasMany(models.Contribution, {
        foreignKey: {
          allowNull: false,
          unique: 'ContributorId_StoryId'
        },
        onDelete: 'CASCADE'
      });

      Story.belongsToMany(models.Member, {
        as: 'Contributors',
        through: models.Contribution
      });
    }
  }
});

And my join model:

const Contribution = sequelize.define('Contribution', {
  id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: DataTypes.INTEGER
  }
}, {
  classMethods: {
    associate: (models) => {
      Contribution.belongsTo(models.Member, {
        as: 'Contributor',
        foreignKey: {
          allowNull: false,
          unique: 'ContributorId_StoryId'
        },
        onDelete: 'CASCADE'
      });

      Contribution.belongsTo(models.Story, {
        foreignKey: {
          allowNull: false,
          unique: 'ContributorId_StoryId'
        },
        onDelete: 'CASCADE'
      });
    }
  }
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

12reactions
MauriceButlercommented, Mar 14, 2017

Cannot read property ‘_pseudo’ of undefined caused by non-existent model in include (typo in model name)

11reactions
Johnselcommented, Mar 24, 2016

I had this error just now, turned out to be me trying to include a non-existent model in the findAll()

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL join two tables related by a single column primary key or ...
In this page we are going to discuss the usage of two or more tables. in a joining with single column PRIMARY KEY...
Read more >
SQL joins and how to use them - Launch School
The JOIN is based on the conditions supplied in the ON clause. A LEFT JOIN will always include the rows from the LEFT...
Read more >
SQL JOIN TABLES: Working with Queries in SQL Server
The FULL JOIN clause retrieves all the records from both the tables irrespective of the match between the values in the columns used...
Read more >
How to Join on Multiple Columns | LearnSQL.com
You want to join tables on multiple columns by using a primary compound key in one table and a foreign compound key in...
Read more >
Using joins to combine data from different tables in MySQL
When combining tables, the join condition determines how rows will be matched together to form the composite results. The basic premise is to...
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