Created
November 22, 2017 18:41
-
-
Save malikid/6a859e1c388aef81d60d671cae3a686d to your computer and use it in GitHub Desktop.
Notes for ORM Sequelize - required field while joining tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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