3

I have messed around with trying to get some default data for my application to seed into my PostgreSQL database using sequelize. So far I have only found one way to do it, and it's not ideal.

My main question is if anyone here knows the "best practice" way to do it.

So far I have tried using Seeds, injecting the default information after syncing each model, and finally I have fallen back to requiring files that define how I want to inject the default information aftermodels.sequelize.sync() in the main app.js file.

Seeding: This method seems to fall apart when doing much more than dumping data into the database, associations, namely the classmethods, (to my knowledge) cannot be used here which makes updating a Many-To-Many relationship nearly impossible.

Model Definition: The issue with injecting default information with associations into the model is that you have to somehow import the other model definitions into the model, which I couldn't seem to get working right.

At this point I'm not so interesting in picking apart code as much as I am seeking what you would think/know to be the best way to get default data that has associations into the database. It seems like this would be a more popular issue since almost every app ships with at least a few pieces of default information. I would love to be told that I completely overlooked something and it's actually pretty simple :)

Thanks for the discussion in advance.

joshrathke
  • 7,564
  • 7
  • 23
  • 38
  • Do you have associations already defined in your model or you want to create new associations ? – AbhinavD Jul 20 '17 at 04:29
  • I already have working associations defined within the model(s). – joshrathke Jul 20 '17 at 04:38
  • I guess my primary questions is how do people deal with more complex seeding scenarios. For instance I am working on a User/User Role system on an app that will be installed on raspberry pies, so I need to be able to quickly seed a few User Roles that require associations to User Permissions. I couldn't get that to happen with the onboard seed process. – joshrathke Jul 20 '17 at 04:42

1 Answers1

0

We don't need to do anything extra if we have our model associations defined already. Lets take an example where doctor can have more than one speciality. In the model definition, we can define our associations.

classMethods: {
      associate: function (models) {
        models.doctor.hasMany(models.specialty, {foreignKey: 'doctor_npi'});
      }

Now we can create an insert function and call this from seeding

insertDoctor: function (models, firstname, lastname, specialties) {
        return this.create({
          firstname: firstname,
          lastname: lastname,
          specialties: specialties,
        }, {
          include: [models.specialty],
        });
      }

Calling this function with right parameters should work.

There are more examples in the official documentation

AbhinavD
  • 6,892
  • 5
  • 30
  • 40
  • Right, but what about a many to many association that goes through a join table. Normally I would use the SetSpecialities() function, but it's not available within a seed. – joshrathke Jul 20 '17 at 14:06
  • @joshrathke Did you figure out a workaround for this? – Rowland May 17 '18 at 03:11