0

Hi Folks, I have these two models:

const media = sequelize.define('media', {
              id: { type: Sequelize.UUID, primaryKey: true, defaultValue: Sequelize.UUIDV4 },
              name: { type: Sequelize.STRING, allowNull: false, unique:'compositeIndex' },}, { freezeTableName: true });

const mediaGenre = sequelize.define('mediaGenre', {
                  id: { type: Sequelize.UUID, primaryKey: true, defaultValue: Sequelize.UUIDV4 }}, { freezeTableName: true, name: { plural: 'mediaGenre' } });

and these two relations:

media.hasMany(mediaGenre, as: 'mGenre');
mediaGenre.belongsTo(media);

media.belongsToMany(genre, { through: mediaGenre });
genre.belongsToMany(media, { through: mediaGenre });

when I want to create a media I do:

MediaModels.media.create(
            {name: 'Hulk',
            mediaGenre: [
            { genreId: '021baab5-7fc6-4b06-aca5-e4b1ed1f3ce4' },
            { genreId: '03f069a4-dc52-4ab5-82d3-6bcd67d2d29e' }]},
            {
                include: [
                    {model: MediaModels.mediaGenre, as: 'mGenre'}
                ]
            }
        );

These has been working with sequelize 3 and recently I updated to sequelize 4.4.2 and it is not throwing error but the table mediaGenre it's not been populated.

Any ideas of what could be the error?

Lucas Assmann
  • 41
  • 1
  • 8
  • Did you try referencing the `association`? Check this out: http://docs.sequelizejs.com/manual/tutorial/associations.html#creating-with-associations. I think the syntax has changed slightly since 3.x.x. I can prepare a more detailed answer if you want :). – Dylan Aspden Aug 10 '17 at 20:51
  • yes, I tried also – Lucas Assmann Aug 11 '17 at 16:10

1 Answers1

0

Have you tried to sync after you referenced the association? I had this issue and when i called to sync (with force false, this is important if tou dont want to erase your schema and to be recreated), the junction table was created.

If you are using sequelize-cli,also notice that in this version, classMethods are not longer supported. Instead, you shold add a method called associate directly to your model, which will be called in your models/index.js.