Skip to content

Instantly share code, notes, and snippets.

@malikid
Created November 22, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malikid/6a859e1c388aef81d60d671cae3a686d to your computer and use it in GitHub Desktop.
Save malikid/6a859e1c388aef81d60d671cae3a686d to your computer and use it in GitHub Desktop.
Notes for ORM Sequelize - required field while joining tables
// required: true - Sequelize Code
test = (guideId) => this.Manual.findAll({
include: [
{model: this.Topic, required: true, include: [
{model: this.Guide, where: {id: guideId}}
]}
]
});
// required: true - Interpreted SQL (Modified for being readable)
SELECT "manual".*, "topics".*, "topics->guides".*
FROM "test"."manuals" AS "manual"
INNER JOIN "test"."topics" AS "topics" ON "manual"."id" = "topics"."manual_id"
INNER JOIN "test"."guides" AS "topics->guides" ON "topics"."id" = "topics->guides"."topic_id" AND "topics->guides"."id" = '37abff8e-003f-431a-8712-fe61c03f31f3';
// Without required - Sequelize Code
test = (guideId) => this.Manual.findAll({
include: [
{model: this.Topic, include: [
{model: this.Guide, where: {id: guideId}}
]}
]
});
// Without required - Interpreted SQL (Modified for being readable)
SELECT "manual".*, "topics".*, "topics->guides".*
FROM "test"."manuals" AS "manual"
LEFT OUTER JOIN "test"."topics" AS "topics" ON "manual"."id" = "topics"."manual_id"
INNER JOIN "test"."guides" AS "topics->guides" ON "topics"."id" = "topics->guides"."topic_id" AND "topics->guides"."id" = '37abff8e-003f-431a-8712-fe61c03f31f3';
// required: false - Sequelize Code
test = (guideId) => this.Manual.findAll({
include: [
{model: this.Topic, include: [
{model: this.Guide, required: false, where: {id: guideId}}
]}
]
});
// required: false - Interpreted SQL (Modified for being readable)
SELECT "manual".*, "topics".*, "topics->guides".*
FROM "test"."manuals" AS "manual"
LEFT OUTER JOIN "test"."topics" AS "topics" ON "manual"."id" = "topics"."manual_id"
LEFT OUTER JOIN "test"."guides" AS "topics->guides" ON "topics"."id" = "topics->guides"."topic_id" AND "topics->guides"."id" = '37abff8e-003f-431a-8712-fe61c03f31f3';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment