seed or insert using associations

666 views
Skip to first unread message

Nacho Caballero

unread,
Oct 24, 2015, 10:02:19 PM10/24/15
to Sequelize
Hi, I'm trying to figure out the best way to bulkCreate records with foreign keys. Can somebody please give me a short example? I don't know if I should use something like this:

Post.bulkCreate([
        { name: 'blah'},
        { name: 'bleh'},
      ]).then(function(posts){
          console.log(posts.length + " created");
          posts.map(function(p){return p.addComment({name: "blahblah"});
      })

And should I have this in posts.js or in a migration or seed? If so, how do I access Post from the migration? I'm pretty confused and I haven't been able to find the answer in the documentation. 



Mick Hansen

unread,
Oct 25, 2015, 1:39:42 PM10/25/15
to Nacho Caballero, Sequelize
I don't really know of any established patterns for seeding databases.
As for accessing your models from a migration you should be able to import your sequelize models like in any other file since it's just a node.js file.

As for the best way to seed, that's really up to you, you can also use a series of nested create calls.
--
Mick Hansen
@mhansendev
mhansen.io
Reply all
Reply to author
Forward
0 new messages