0

I have created a seeder file using sequelize-cli to bulkInsert data into a mysql db and it works all fine. However, one of the fields(userId) that i'm inserting is a value present in another table(User).

What I was planning to do is - Run a seed which inserts data into User table first and then run my second seeder file.

My doubt - I can create the seeder files separately but how do I run those in sequence .ie. make sure the "user" seeder file runs and completes before I start seeding the second via sequelize-cli ?

Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44
  • check this post out about the order of migration execution order since it also applies to seeders [Sequelize migration order of execution](https://stackoverflow.com/questions/33429383/sequelize-migration-order-of-execution) (possible duplicate) – mohdule Sep 03 '19 at 11:14
  • You may also achieve the desired order of execution by manually renaming your seeder files to be in the order you want, for example you can append a numbered prefix like so `1-users-table-seeder.js` `2-other-table-seeder.js` – mohdule Sep 03 '19 at 11:23
  • Thanks ... this is great help! Also this would mean, we have to group the files now in a folder rather than choosing which one to pre-run. ie. What if I wanted to pre-run a seeder file from another folder(lets say its needed for the other seeder file as well) ? – Nikhil Nanjappa Sep 03 '19 at 11:58
  • You are welcome ... Typically you have a `.sequelizerc` in which you define paths for your (config, migrations, models and seeders) in order to generate a `config.json` (or .js) file by running `sequelize init`, then when you run a sequelize command (for example: `sequelize db:seed:all`) Sequelize will automatically look inside the appropriate paths based on your config. – mohdule Sep 03 '19 at 12:42

1 Answers1

0

If anyone else is looking for the answer.

As far as I have seen, itseems there are 2 options for this as of now:

  1. Create a separate seeder files and manually name them in sequence ie. 1-user-seeder.js and 2-application-seeder.js. As @Mohdule mentioned in the comments above. OR.
  2. Import the Sequelize model and create the user within your primary seeder file. Based on this comment.
Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44