assotiating two tables in sequelize









up vote
-1
down vote

favorite












Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.



I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
There can be One Zone having many PLZs and one PLZ can belong to One zone.



On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id



Zone Model:






const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');


const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false

,
timestamps: true
);


module.exports = Zone;





PLZ model:






const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');


const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING


);


module.exports = PLZ;





and this is the zone_plz model:






const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');


const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',

,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',


,
timestamps: true
);


module.exports = zone_plz;





and this is how I query it:






router.get('/', function (req, res, next) 

zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,

model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);




As you can see i want to return a zone with all the belonging plzs to the user.

Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!



Can anyone help me?










share|improve this question

























    up vote
    -1
    down vote

    favorite












    Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.



    I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
    There can be One Zone having many PLZs and one PLZ can belong to One zone.



    On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id



    Zone Model:






    const Sequelize = require('sequelize');
    const sequelize = require('../util/database');
    const PLZ = require('../models/PLZ');
    const zone_plz = require('../models/relations/zone_plzs');


    const Zone = sequelize.define('zone',
    name:
    type: Sequelize.STRING,
    allowNull: false
    ,
    color:
    type: Sequelize.STRING,
    allowNull: false

    ,
    timestamps: true
    );


    module.exports = Zone;





    PLZ model:






    const Sequelize = require('sequelize');
    const sequelize = require('../util/database');
    const Zone = require('../models/Zone');
    const zone_plz = require('../models/relations/zone_plzs');


    const PLZ = sequelize.define('plz',
    plz:
    type: Sequelize.INTEGER,
    allowNull: false
    ,
    city:
    type: Sequelize.STRING,
    allowNull: false
    ,
    district:
    type: Sequelize.STRING


    );


    module.exports = PLZ;





    and this is the zone_plz model:






    const Sequelize = require('sequelize');
    const sequelize = require('../../util/database');
    const PLZ = require('../../models/PLZ');
    const Zone = require('../../models/Zone');


    const zone_plz = sequelize.define('zone_plz',
    zone_id:
    type: Sequelize.INTEGER,
    allowNull: false,
    references:
    model: Zone,
    key: 'id',

    ,
    plz_id:
    type: Sequelize.INTEGER,
    allowNull: false,
    references:
    model: PLZ,
    key: 'id',


    ,
    timestamps: true
    );


    module.exports = zone_plz;





    and this is how I query it:






    router.get('/', function (req, res, next) 

    zone_plz.findAll(
    include: [
    model: PLZ,
    as: 'plz'
    ,

    model: Zone,
    as: 'zone'
    ]
    ).then((result) =>
    res.send(status: true, data: result)
    ).catch(function (err)
    next(err)
    )
    );




    As you can see i want to return a zone with all the belonging plzs to the user.

    Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!



    Can anyone help me?










    share|improve this question























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.



      I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
      There can be One Zone having many PLZs and one PLZ can belong to One zone.



      On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id



      Zone Model:






      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const PLZ = require('../models/PLZ');
      const zone_plz = require('../models/relations/zone_plzs');


      const Zone = sequelize.define('zone',
      name:
      type: Sequelize.STRING,
      allowNull: false
      ,
      color:
      type: Sequelize.STRING,
      allowNull: false

      ,
      timestamps: true
      );


      module.exports = Zone;





      PLZ model:






      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const Zone = require('../models/Zone');
      const zone_plz = require('../models/relations/zone_plzs');


      const PLZ = sequelize.define('plz',
      plz:
      type: Sequelize.INTEGER,
      allowNull: false
      ,
      city:
      type: Sequelize.STRING,
      allowNull: false
      ,
      district:
      type: Sequelize.STRING


      );


      module.exports = PLZ;





      and this is the zone_plz model:






      const Sequelize = require('sequelize');
      const sequelize = require('../../util/database');
      const PLZ = require('../../models/PLZ');
      const Zone = require('../../models/Zone');


      const zone_plz = sequelize.define('zone_plz',
      zone_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: Zone,
      key: 'id',

      ,
      plz_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: PLZ,
      key: 'id',


      ,
      timestamps: true
      );


      module.exports = zone_plz;





      and this is how I query it:






      router.get('/', function (req, res, next) 

      zone_plz.findAll(
      include: [
      model: PLZ,
      as: 'plz'
      ,

      model: Zone,
      as: 'zone'
      ]
      ).then((result) =>
      res.send(status: true, data: result)
      ).catch(function (err)
      next(err)
      )
      );




      As you can see i want to return a zone with all the belonging plzs to the user.

      Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!



      Can anyone help me?










      share|improve this question













      Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.



      I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
      There can be One Zone having many PLZs and one PLZ can belong to One zone.



      On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id



      Zone Model:






      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const PLZ = require('../models/PLZ');
      const zone_plz = require('../models/relations/zone_plzs');


      const Zone = sequelize.define('zone',
      name:
      type: Sequelize.STRING,
      allowNull: false
      ,
      color:
      type: Sequelize.STRING,
      allowNull: false

      ,
      timestamps: true
      );


      module.exports = Zone;





      PLZ model:






      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const Zone = require('../models/Zone');
      const zone_plz = require('../models/relations/zone_plzs');


      const PLZ = sequelize.define('plz',
      plz:
      type: Sequelize.INTEGER,
      allowNull: false
      ,
      city:
      type: Sequelize.STRING,
      allowNull: false
      ,
      district:
      type: Sequelize.STRING


      );


      module.exports = PLZ;





      and this is the zone_plz model:






      const Sequelize = require('sequelize');
      const sequelize = require('../../util/database');
      const PLZ = require('../../models/PLZ');
      const Zone = require('../../models/Zone');


      const zone_plz = sequelize.define('zone_plz',
      zone_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: Zone,
      key: 'id',

      ,
      plz_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: PLZ,
      key: 'id',


      ,
      timestamps: true
      );


      module.exports = zone_plz;





      and this is how I query it:






      router.get('/', function (req, res, next) 

      zone_plz.findAll(
      include: [
      model: PLZ,
      as: 'plz'
      ,

      model: Zone,
      as: 'zone'
      ]
      ).then((result) =>
      res.send(status: true, data: result)
      ).catch(function (err)
      next(err)
      )
      );




      As you can see i want to return a zone with all the belonging plzs to the user.

      Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!



      Can anyone help me?






      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const PLZ = require('../models/PLZ');
      const zone_plz = require('../models/relations/zone_plzs');


      const Zone = sequelize.define('zone',
      name:
      type: Sequelize.STRING,
      allowNull: false
      ,
      color:
      type: Sequelize.STRING,
      allowNull: false

      ,
      timestamps: true
      );


      module.exports = Zone;





      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const PLZ = require('../models/PLZ');
      const zone_plz = require('../models/relations/zone_plzs');


      const Zone = sequelize.define('zone',
      name:
      type: Sequelize.STRING,
      allowNull: false
      ,
      color:
      type: Sequelize.STRING,
      allowNull: false

      ,
      timestamps: true
      );


      module.exports = Zone;





      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const Zone = require('../models/Zone');
      const zone_plz = require('../models/relations/zone_plzs');


      const PLZ = sequelize.define('plz',
      plz:
      type: Sequelize.INTEGER,
      allowNull: false
      ,
      city:
      type: Sequelize.STRING,
      allowNull: false
      ,
      district:
      type: Sequelize.STRING


      );


      module.exports = PLZ;





      const Sequelize = require('sequelize');
      const sequelize = require('../util/database');
      const Zone = require('../models/Zone');
      const zone_plz = require('../models/relations/zone_plzs');


      const PLZ = sequelize.define('plz',
      plz:
      type: Sequelize.INTEGER,
      allowNull: false
      ,
      city:
      type: Sequelize.STRING,
      allowNull: false
      ,
      district:
      type: Sequelize.STRING


      );


      module.exports = PLZ;





      const Sequelize = require('sequelize');
      const sequelize = require('../../util/database');
      const PLZ = require('../../models/PLZ');
      const Zone = require('../../models/Zone');


      const zone_plz = sequelize.define('zone_plz',
      zone_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: Zone,
      key: 'id',

      ,
      plz_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: PLZ,
      key: 'id',


      ,
      timestamps: true
      );


      module.exports = zone_plz;





      const Sequelize = require('sequelize');
      const sequelize = require('../../util/database');
      const PLZ = require('../../models/PLZ');
      const Zone = require('../../models/Zone');


      const zone_plz = sequelize.define('zone_plz',
      zone_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: Zone,
      key: 'id',

      ,
      plz_id:
      type: Sequelize.INTEGER,
      allowNull: false,
      references:
      model: PLZ,
      key: 'id',


      ,
      timestamps: true
      );


      module.exports = zone_plz;





      router.get('/', function (req, res, next) 

      zone_plz.findAll(
      include: [
      model: PLZ,
      as: 'plz'
      ,

      model: Zone,
      as: 'zone'
      ]
      ).then((result) =>
      res.send(status: true, data: result)
      ).catch(function (err)
      next(err)
      )
      );





      router.get('/', function (req, res, next) 

      zone_plz.findAll(
      include: [
      model: PLZ,
      as: 'plz'
      ,

      model: Zone,
      as: 'zone'
      ]
      ).then((result) =>
      res.send(status: true, data: result)
      ).catch(function (err)
      next(err)
      )
      );






      mysql node.js foreign-keys associations sequelize.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 18:32









      b0ss

      255




      255






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
          then create associations between plz & zone like:



          plz.belongsToMany(zone, through: 'zone_plz');
          zone.belongsToMany(plz, through: 'zone_plz');





          share|improve this answer




















          • Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
            – b0ss
            Nov 13 at 19:18











          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',
          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%2f53251874%2fassotiating-two-tables-in-sequelize%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
          then create associations between plz & zone like:



          plz.belongsToMany(zone, through: 'zone_plz');
          zone.belongsToMany(plz, through: 'zone_plz');





          share|improve this answer




















          • Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
            – b0ss
            Nov 13 at 19:18















          up vote
          0
          down vote













          You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
          then create associations between plz & zone like:



          plz.belongsToMany(zone, through: 'zone_plz');
          zone.belongsToMany(plz, through: 'zone_plz');





          share|improve this answer




















          • Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
            – b0ss
            Nov 13 at 19:18













          up vote
          0
          down vote










          up vote
          0
          down vote









          You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
          then create associations between plz & zone like:



          plz.belongsToMany(zone, through: 'zone_plz');
          zone.belongsToMany(plz, through: 'zone_plz');





          share|improve this answer












          You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
          then create associations between plz & zone like:



          plz.belongsToMany(zone, through: 'zone_plz');
          zone.belongsToMany(plz, through: 'zone_plz');






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 4:53









          Dmytro Mysak

          325213




          325213











          • Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
            – b0ss
            Nov 13 at 19:18

















          • Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
            – b0ss
            Nov 13 at 19:18
















          Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
          – b0ss
          Nov 13 at 19:18





          Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
          – b0ss
          Nov 13 at 19:18


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53251874%2fassotiating-two-tables-in-sequelize%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線