0

I have some promise chain to add data. If I use it "normally" in a JavaScript file, I start with Node.js or call it some other way, it works (more or less - different question).

But if I want to use it at a sequelize seed file with the sequelize-cli (sequelize db:seed:all), the promises won't work. OK, there's an error that runs into the .catch chain, but the .then won't work...

The promise fields are all undefined...

I know I can use bulkInsert, but why do I use an ORM if I "have to" use raw SQL, especially if I have links between the tables that point to uuid's?

I defined the models, had a "normal" migrate file, and want the "start" data with a seed. Do I need something separate to use create / bulkCreate promises?

Did I miss something in the documentation?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cybeaer
  • 43
  • 9

1 Answers1

0

I found it myself with many tries and the answer of this: Sequelize dynamic seeding

the associations needed to be fixed at the models:

User.associate = function(models) {
  User.belongsToMany(
    models.Role, {
      through: 'user_role',
    }
  );
};

and

Role.associate = function(models) {
  Role.belongsToMany(
    models.User, {
      through: 'user_role',
    }
  );
};

and the m:n table needed this:

....
.then(() => {
  queryInterface.createTable('user_role', {
    userUuid: {
      type: Sequelize.UUIDV4,
      references: {
        model: 'User',
        key: 'uuid',
      },
      allowNull: false,
    },
    roleUuid: {
      type: Sequelize.UUIDV4,
      references: {
        model: 'Role',
        key: 'uuid',
      },
      allowNull: false,
    },
    createdAt: { allowNull: false, type: Sequelize.DATE },
    updatedAt: { allowNull: false, type: Sequelize.DATE },
  });
}).then(() => {
  return queryInterface.addConstraint(
    'user_role',
    ['userUuid', 'roleUuid'], {
      unique: true,
      type: 'primary key',
      name: 'userrole_pkey',
    }
  );
});

renamed the fields to be like the generated ones from sequelize

Cybeaer
  • 43
  • 9