用关联序列化创建对象 [英] Sequelize create object with associations

查看:100
本文介绍了用关联序列化创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将序列化模型与它们的关联保存起来.所有的关联都是一对一的.从数据库中检索具有关联的模型可以很好地工作,但是插入它们是另一回事,而文档却使我更加困惑.

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.

这是我的插入方法:

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

在文档中以及在stackoverflow上,我已经看到了很多实现此目的的方法,到目前为止,对我来说,这些方法都不可行.有人在照顾我吗?

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?

推荐答案

我总是通过外键保存带有关联的模型

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

例如

models.user.create({ name : 'test', usergroup_id : 1 });

这篇关于用关联序列化创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆