jimthedev / seedquelize

Seed your database with Sequelize
3 stars 1 forks source link

Does this work for associations? #1

Open georgeedwards opened 7 years ago

georgeedwards commented 7 years ago

Say your example was Users and Todos (seems to be a demo favourite!), where a user hasMany Todos and a Todo hasOne User. Presumably you can't know the id of the User before it is created, in which case associating a Todo with a user is tricky?

Does this package support this, in which case a note in the docs would be great, otherwise, is that possible/ in scope?

jimthedev commented 7 years ago

Hey I just saw this. For some reason I got swamped with notifications and missed it.

This project assumes that if you will manually provide the ids involved in relationships. We should probably make a note in the readme to indicate how you can do this, but essentially it comes down to providing either an array of ids on the 'many' side, or a single id on the 'one' side.

We don't force you to put ids on individual items being added to tables since generally they are auto generated. So, items in a table will be created in the order that they fall in the array that you provide. So if you have three users then the item 0 in the array would be auto assigned id 1 by your database. A user at index 1 in the provided array would have an id of 2, etc. So we can actually know the ids that will be created given the order of the item in an array. I think it probably reasonable to ask for an example of this so I'll put it in my personal backlog to add and will keep this open until it is complete.

Thanks!