GrimurD GrimurD - 4 years ago 967
SQL Question

Sequelize create object with associations

I'm trying to save sequelize models with their associations. All the associations are one to one. Retrieving models with associations from the database works just fine but inserting them is another matter and the documentation is just making me more confused.

Here's my insert method:

.create(user, {
include: [{model: models.skraningar}, {model: models.radusergroup}, {model: models.radippool}]
.then(success, error);

I've seen so many ways to do this both in the documentation and here on stackoverflow and none of them make sense to me so far. Anyone care to clear things up for me?

Answer Source

I always save a model with association by it's foreign key


models.user.create({ name : 'test', usergroup_id : 1 });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download