Sequelize Create/Post with M:N



我试图实现一个M:N关联与Sequelize。为此,我查看了文档(doc),发现了与我的用例非常相似的内容:

User.belongsToMany(Profile, { through: User_Profile });
Profile.belongsToMany(User, { through: User_Profile });
现在我想有一个端点,它允许我添加一个新的拥有列表的用户已存在

配置文件。(或者反过来)。所以如。{"id" 4"username"p4dm3"profiles"({"id" 6},{"id" 4]}然而,据我所知,文档只提到不存在包含语句的概要文件的情况。当然,我可以先创建一个新用户,然后在下一步创建关联,但我认为对于事务处理来说,一步完成会更方便。此外,我的问题与这里描述的问题非常相似。提前谢谢。

如果没有用户,则必须首先创建该用户,然后可以使用sequelize helper将现有配置文件保存到该用户:

const profiles = [profiles]
const user = await User.create(req.body)
user.addProfiles(profiles)

最新更新