I'm writing tests for my project. It uses Sequelize and I thought about doing the following:
"pretest": "NODE_ENV=testing yarn sequelize db:migrate && yarn sequelize db:seed:all",
"test": "mocha --require @babel/register 'src/tests/**/*.spec.js'",
"posttest": "NODE_ENV=testing yarn sequelize db:migrate:undo:all"
But the following shows:
❯ yarn test
yarn run v1.19.2
$ NODE_ENV=testing yarn sequelize db:migrate && yarn sequelize db:seed:all
$ /home/gabriel/Workspace/graphql-apollo/node_modules/.bin/sequelize db:migrate
Sequelize CLI [Node: 12.13.1, CLI: 5.5.1, ORM: 5.21.2]
Loaded configuration file "src/config/database.js".
== 20191123132531-create-users: migrating =======
== 20191123132531-create-users: migrated (0.047s)
== 20191123132658-create-messages: migrating =======
== 20191123132658-create-messages: migrated (0.028s)
$ /home/gabriel/Workspace/graphql-apollo/node_modules/.bin/sequelize db:seed:all
Sequelize CLI [Node: 12.13.1, CLI: 5.5.1, ORM: 5.21.2]
Loaded configuration file "src/config/database.js".
== 20191123132945-users: migrating =======
ERROR: Validation error
error Command failed with exit code 1.
If I execute the migration and seeding command separately, it works fine. I also tried to use concurrently
, but the same happens.
"pretest": "concurrently 'NODE_ENV=testing yarn sequelize db:migrate' 'yarn sequelize db:seed:all'",
@gamofe Add the --debug
flag to the command to see more info about the error. e.g.
$ sequelize db:seed:all --debug
It's very likely you are getting this error because you are running sequelize db:seed:all
without undoing previous seed. If your table already contains the data you are trying to seed in which some of them have unique constraint, you'll hit the unique constraint error.
To solve this you would need to run the sequelize db:seed:undo:all
before seeding.
You can find more info here https://sequelize.org/master/manual/migrations.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With