Using a column from a join table in the primary WHERE clause
See original GitHub issueHi, 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:
- Created 8 years ago
- Reactions:2
- Comments:20 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Cannot read property ‘_pseudo’ of undefined caused by non-existent model in include (typo in model name)
I had this error just now, turned out to be me trying to include a non-existent model in the findAll()