Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

models
    .radcheck
    .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?

like image 855
grimurd Avatar asked Mar 18 '23 07:03

grimurd


1 Answers

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

e.g

models.user.create({ name : 'test', usergroup_id : 1 });
like image 121
Sergio Marcelino Avatar answered Mar 19 '23 20:03

Sergio Marcelino