Sequelize Join table1 with table[X] referenced in table1.column












0















So I have:




  • table1=vehicles

  • table2=cars

  • table3=motocycle

  • table4=trucks...


In table1, I have a column type which references by number one of the above tables (table2 | table3 | table4...). I would like to list all vehicles in table1 and join the table referenced in type; but I find that to be a bit hard the only way I find is to get all vehicles and loop through all and for each get the complementary data from the appropriate table specified in type.



For example:



vehicles:



id(0)------------type(1)---------------name(dacia)

id(1)------------type(2)---------------name(porsche)

id(2)------------type(2)---------------name(hummer)

id(3)------------type(3)---------------name(kongo)

id(4)------------type(3)---------------name(renault)


cars(type 1):



id(0)----------vehicleId(0)-------otherInfo(new)


trucks(type 2):



id(0)----------vehicleId(1)-------otherInfo(used)

id(1)----------vehicleId(2)-------otherInfo(like_new)


motocycle(type 3):



id(0)----------vehicleId(3)-------otherInfo(old)

id(1)----------vehicleId(4)-------otherInfo(very_old)


Query Result:



id(0)------------type(1)---------------name(dacia)-------otherInfo(new)

id(1)------------type(2)---------------name(porsche)-------otherInfo(used)

id(2)------------type(2)---------------name(hummer)-------otherInfo(like_new)

id(3)------------type(3)---------------name(kongo)-------otherInfo(old)

id(4)------------type(3)---------------name(renault)-------otherInfo(very_old)









share|improve this question





























    0















    So I have:




    • table1=vehicles

    • table2=cars

    • table3=motocycle

    • table4=trucks...


    In table1, I have a column type which references by number one of the above tables (table2 | table3 | table4...). I would like to list all vehicles in table1 and join the table referenced in type; but I find that to be a bit hard the only way I find is to get all vehicles and loop through all and for each get the complementary data from the appropriate table specified in type.



    For example:



    vehicles:



    id(0)------------type(1)---------------name(dacia)

    id(1)------------type(2)---------------name(porsche)

    id(2)------------type(2)---------------name(hummer)

    id(3)------------type(3)---------------name(kongo)

    id(4)------------type(3)---------------name(renault)


    cars(type 1):



    id(0)----------vehicleId(0)-------otherInfo(new)


    trucks(type 2):



    id(0)----------vehicleId(1)-------otherInfo(used)

    id(1)----------vehicleId(2)-------otherInfo(like_new)


    motocycle(type 3):



    id(0)----------vehicleId(3)-------otherInfo(old)

    id(1)----------vehicleId(4)-------otherInfo(very_old)


    Query Result:



    id(0)------------type(1)---------------name(dacia)-------otherInfo(new)

    id(1)------------type(2)---------------name(porsche)-------otherInfo(used)

    id(2)------------type(2)---------------name(hummer)-------otherInfo(like_new)

    id(3)------------type(3)---------------name(kongo)-------otherInfo(old)

    id(4)------------type(3)---------------name(renault)-------otherInfo(very_old)









    share|improve this question



























      0












      0








      0








      So I have:




      • table1=vehicles

      • table2=cars

      • table3=motocycle

      • table4=trucks...


      In table1, I have a column type which references by number one of the above tables (table2 | table3 | table4...). I would like to list all vehicles in table1 and join the table referenced in type; but I find that to be a bit hard the only way I find is to get all vehicles and loop through all and for each get the complementary data from the appropriate table specified in type.



      For example:



      vehicles:



      id(0)------------type(1)---------------name(dacia)

      id(1)------------type(2)---------------name(porsche)

      id(2)------------type(2)---------------name(hummer)

      id(3)------------type(3)---------------name(kongo)

      id(4)------------type(3)---------------name(renault)


      cars(type 1):



      id(0)----------vehicleId(0)-------otherInfo(new)


      trucks(type 2):



      id(0)----------vehicleId(1)-------otherInfo(used)

      id(1)----------vehicleId(2)-------otherInfo(like_new)


      motocycle(type 3):



      id(0)----------vehicleId(3)-------otherInfo(old)

      id(1)----------vehicleId(4)-------otherInfo(very_old)


      Query Result:



      id(0)------------type(1)---------------name(dacia)-------otherInfo(new)

      id(1)------------type(2)---------------name(porsche)-------otherInfo(used)

      id(2)------------type(2)---------------name(hummer)-------otherInfo(like_new)

      id(3)------------type(3)---------------name(kongo)-------otherInfo(old)

      id(4)------------type(3)---------------name(renault)-------otherInfo(very_old)









      share|improve this question
















      So I have:




      • table1=vehicles

      • table2=cars

      • table3=motocycle

      • table4=trucks...


      In table1, I have a column type which references by number one of the above tables (table2 | table3 | table4...). I would like to list all vehicles in table1 and join the table referenced in type; but I find that to be a bit hard the only way I find is to get all vehicles and loop through all and for each get the complementary data from the appropriate table specified in type.



      For example:



      vehicles:



      id(0)------------type(1)---------------name(dacia)

      id(1)------------type(2)---------------name(porsche)

      id(2)------------type(2)---------------name(hummer)

      id(3)------------type(3)---------------name(kongo)

      id(4)------------type(3)---------------name(renault)


      cars(type 1):



      id(0)----------vehicleId(0)-------otherInfo(new)


      trucks(type 2):



      id(0)----------vehicleId(1)-------otherInfo(used)

      id(1)----------vehicleId(2)-------otherInfo(like_new)


      motocycle(type 3):



      id(0)----------vehicleId(3)-------otherInfo(old)

      id(1)----------vehicleId(4)-------otherInfo(very_old)


      Query Result:



      id(0)------------type(1)---------------name(dacia)-------otherInfo(new)

      id(1)------------type(2)---------------name(porsche)-------otherInfo(used)

      id(2)------------type(2)---------------name(hummer)-------otherInfo(like_new)

      id(3)------------type(3)---------------name(kongo)-------otherInfo(old)

      id(4)------------type(3)---------------name(renault)-------otherInfo(very_old)






      mysql sql node.js sequelize.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 1:54









      TeeKea

      3,22851832




      3,22851832










      asked Nov 27 '18 at 21:54









      Abouhassane AbdelhamidAbouhassane Abdelhamid

      238




      238
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You could use LEFT JOIN clauses that include a condition on the vehicule type, and then use CONCAT/COALESCE to return the (only) matching value, like so :



          select
          v.id,
          v.type,
          v.name,
          concat(
          coalesce( c.otherInfo, ''),
          coalesce( t.otherInfo, ''),
          coalesce( m.otherInfo, '')
          ) as otherInfo
          from vehicules v
          left join cars c on c.vehiculeId = v.id and v.type = 1
          left join trucks t on t.vehiculeId = v.id and v.type = 2
          left join motocycle m on m.vehiculeId = v.id and v.type = 3





          share|improve this answer































            0














            I think you need to read this first : Associations



            This is how you can define the relations b/w two models/tables , like





            • BelongsTo

            • Belongs-To-Many

            • HasOne

            • hasMany




            Once you are done with this, then you can query the model to get realted data as you are expecting. example



            // Find all projects with a least one task where task.state === project.state
            Project.findAll({
            include: [{
            model: Task
            // you can go nested too , as per your requirment
            }]
            })



            This is how you should be doing when you are using the sequelize.js







            share|improve this answer
























            • yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

              – Abouhassane Abdelhamid
              Nov 28 '18 at 7:56











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508782%2fsequelize-join-table1-with-tablex-referenced-in-table1-column%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You could use LEFT JOIN clauses that include a condition on the vehicule type, and then use CONCAT/COALESCE to return the (only) matching value, like so :



            select
            v.id,
            v.type,
            v.name,
            concat(
            coalesce( c.otherInfo, ''),
            coalesce( t.otherInfo, ''),
            coalesce( m.otherInfo, '')
            ) as otherInfo
            from vehicules v
            left join cars c on c.vehiculeId = v.id and v.type = 1
            left join trucks t on t.vehiculeId = v.id and v.type = 2
            left join motocycle m on m.vehiculeId = v.id and v.type = 3





            share|improve this answer




























              0














              You could use LEFT JOIN clauses that include a condition on the vehicule type, and then use CONCAT/COALESCE to return the (only) matching value, like so :



              select
              v.id,
              v.type,
              v.name,
              concat(
              coalesce( c.otherInfo, ''),
              coalesce( t.otherInfo, ''),
              coalesce( m.otherInfo, '')
              ) as otherInfo
              from vehicules v
              left join cars c on c.vehiculeId = v.id and v.type = 1
              left join trucks t on t.vehiculeId = v.id and v.type = 2
              left join motocycle m on m.vehiculeId = v.id and v.type = 3





              share|improve this answer


























                0












                0








                0







                You could use LEFT JOIN clauses that include a condition on the vehicule type, and then use CONCAT/COALESCE to return the (only) matching value, like so :



                select
                v.id,
                v.type,
                v.name,
                concat(
                coalesce( c.otherInfo, ''),
                coalesce( t.otherInfo, ''),
                coalesce( m.otherInfo, '')
                ) as otherInfo
                from vehicules v
                left join cars c on c.vehiculeId = v.id and v.type = 1
                left join trucks t on t.vehiculeId = v.id and v.type = 2
                left join motocycle m on m.vehiculeId = v.id and v.type = 3





                share|improve this answer













                You could use LEFT JOIN clauses that include a condition on the vehicule type, and then use CONCAT/COALESCE to return the (only) matching value, like so :



                select
                v.id,
                v.type,
                v.name,
                concat(
                coalesce( c.otherInfo, ''),
                coalesce( t.otherInfo, ''),
                coalesce( m.otherInfo, '')
                ) as otherInfo
                from vehicules v
                left join cars c on c.vehiculeId = v.id and v.type = 1
                left join trucks t on t.vehiculeId = v.id and v.type = 2
                left join motocycle m on m.vehiculeId = v.id and v.type = 3






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 22:44









                GMBGMB

                16.9k3927




                16.9k3927

























                    0














                    I think you need to read this first : Associations



                    This is how you can define the relations b/w two models/tables , like





                    • BelongsTo

                    • Belongs-To-Many

                    • HasOne

                    • hasMany




                    Once you are done with this, then you can query the model to get realted data as you are expecting. example



                    // Find all projects with a least one task where task.state === project.state
                    Project.findAll({
                    include: [{
                    model: Task
                    // you can go nested too , as per your requirment
                    }]
                    })



                    This is how you should be doing when you are using the sequelize.js







                    share|improve this answer
























                    • yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

                      – Abouhassane Abdelhamid
                      Nov 28 '18 at 7:56
















                    0














                    I think you need to read this first : Associations



                    This is how you can define the relations b/w two models/tables , like





                    • BelongsTo

                    • Belongs-To-Many

                    • HasOne

                    • hasMany




                    Once you are done with this, then you can query the model to get realted data as you are expecting. example



                    // Find all projects with a least one task where task.state === project.state
                    Project.findAll({
                    include: [{
                    model: Task
                    // you can go nested too , as per your requirment
                    }]
                    })



                    This is how you should be doing when you are using the sequelize.js







                    share|improve this answer
























                    • yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

                      – Abouhassane Abdelhamid
                      Nov 28 '18 at 7:56














                    0












                    0








                    0







                    I think you need to read this first : Associations



                    This is how you can define the relations b/w two models/tables , like





                    • BelongsTo

                    • Belongs-To-Many

                    • HasOne

                    • hasMany




                    Once you are done with this, then you can query the model to get realted data as you are expecting. example



                    // Find all projects with a least one task where task.state === project.state
                    Project.findAll({
                    include: [{
                    model: Task
                    // you can go nested too , as per your requirment
                    }]
                    })



                    This is how you should be doing when you are using the sequelize.js







                    share|improve this answer













                    I think you need to read this first : Associations



                    This is how you can define the relations b/w two models/tables , like





                    • BelongsTo

                    • Belongs-To-Many

                    • HasOne

                    • hasMany




                    Once you are done with this, then you can query the model to get realted data as you are expecting. example



                    // Find all projects with a least one task where task.state === project.state
                    Project.findAll({
                    include: [{
                    model: Task
                    // you can go nested too , as per your requirment
                    }]
                    })



                    This is how you should be doing when you are using the sequelize.js








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 28 '18 at 5:40









                    Vivek DoshiVivek Doshi

                    21.9k33552




                    21.9k33552













                    • yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

                      – Abouhassane Abdelhamid
                      Nov 28 '18 at 7:56



















                    • yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

                      – Abouhassane Abdelhamid
                      Nov 28 '18 at 7:56

















                    yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

                    – Abouhassane Abdelhamid
                    Nov 28 '18 at 7:56





                    yes indeed, that's what i do, the only problem here is that the model that i should include depends on the value of type

                    – Abouhassane Abdelhamid
                    Nov 28 '18 at 7:56


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508782%2fsequelize-join-table1-with-tablex-referenced-in-table1-column%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Futebolista

                    Jornalista