0

I'm seeding a table using a model like so

module.exports = {
  up: async function (queryInterface, Sequelize) {
    const company = await Company.create({ name });

Works fine in staging but in production there is an error

20170727141749-admins: migrating =======

Executing (default): INSERT INTO Companies (id,name,createdAt,updatedAt) VALUES (DEFAULT,'Co name','2018-02-28 18:54:37','2018-02-28 18:54:37');

Seed file failed with error: Validation error SequelizeUniqueConstraintError: Validation error

Prod command uses --env production flag like so

sequelize db:seed:all --env production

Staging doesn't use the flag

sequelize db:seed:all

Seems that should not matter. Staging and production apps both run on the same box. NODE_ENV=staging on that box, so the -env flag is being used to target the prod db for the prod deployment. Any ideas?

Community
  • 1
  • 1
1192805
  • 988
  • 2
  • 10
  • 26
  • One thing I think of is that seeds will run again, without undos. If your prod db has already run this seed and has a Company with `name` already in the table, you'll hit the unique constraint error. I'd look into eiher running `db:migrate:undo` (probably not what you want in production) or checking if that record exists. Perhaps use `findOrCreate` depending on your use-case? – vapurrmaid Mar 01 '18 at 20:06
  • 1
    Appreciate the input. It turns out that sequelize db:migrate --env production overrode NODE_ENV but sequelize db:seed:all --env production did not. NODE_ENV was set to staging on the box and seeding was attempting to re-seed staging instead of prod. That has been changed and db is seeded now. – 1192805 Mar 16 '18 at 16:17
  • Thanks for the update on the reason, glad it got resolved! – vapurrmaid Mar 16 '18 at 17:00

0 Answers0