2

I'm using sequelize v5.21 and sequelize-cli v5.5.1 for a MySQL database.

I'm currently trying to create some association 1:M under the following model generated code from the cli like so:

Where: a is the source and b the target.

model.associate = function (models) {
// associations can be defined here
 a.hasOne(models.b);
}

and the other model, i have:

model.associate = function (models) {
// associations can be defined here
 b.belongsTo(models.a);
}

Finally, im using this code to check the associations by creating a entries:

    a.create({
  ...
  }).then(a => {
     return a.createB({
      ...,
    }).catch(err => console.log(err));
   }).catch(err => console.log(err)
   );

In which i'm getting an error on the "a.createB()..." as is not a function...

So, i was curious enough and tried doing the associations right before checking the associations like so:

a.hasOne(b);
b.belongsTo(a);

In which works just fine...

My question is, is the "model.associate" property still working on the v5^?

Note: I have used the check associations method provided in the sequelize documentation but i prefer this one since it's easier to read.

1 Answers1

1

I found nothing about model.associate in Sequelize documentation for v5, but in my project I'm using this code in main models directory:

Object.keys(db).forEach(modelName => {
    if (db[modelName].associate) {
        db[modelName].associate(db);
    }
});

where db is autogenerated by sequelize-cli models imported by sequelize['import']

wmbtrmb
  • 307
  • 1
  • 17