javascript - 如何在 Sequelize 中创建关联
问题描述
我有两个与 belongsTo 关系相关联的 Sequelize 模型。我想在创建用户时创建一个 user_sources 实例,但我正在努力完成它。
模型用户:
const User = sequelize.define('user', {
email: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
password: {
type: Sequelize.STRING,
allowNull: false
}
}, {
tableName: 'users'
})
模型用户来源:
const UserSources = sequelize.define('user_sources', {
abcNews: {
type: Sequelize.BOOLEAN,
},
bbcNews: {
type: Sequelize.BOOLEAN,
}
}, {
tableName: 'user_sources'
})
UserSources.belongsTo(User)
两个模型都已初始化,并且在数据库中正确创建了表。根据 Sequelize 文档,我应该能够在单个查询中创建两个关联,如下所示:
User
.create({
email: user.email,
password: user.password,
}, {
include: UserSources
})
但是,仅创建用户。user_sources 项目不会在表中创建。
不幸的是,该文档仅显示了从子模型创建父模型的示例,而不是相反的示例。我尝试了几种不同的方法,例如使用 hasOne 关联、在 include 中添加模型/关联选项、将数据放入 create 方法等。但我觉得好像我没有正确掌握这个概念。
如果有人能对我的问题有所了解,将不胜感激。谢谢。
解决方案
"sequelize": "^5.21.3"
. 以下是为关联创建数据记录User
和建模的三种方法。此外,我们不断使用to tableUserSources
添加外键约束。userId
user_sources
例如
index.js
:
import { sequelize } from '../../db';
import Sequelize from 'sequelize';
const User = sequelize.define(
'user',
{
email: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
},
{
tableName: 'users',
},
);
const UserSources = sequelize.define(
'user_source',
{
abcNews: {
type: Sequelize.BOOLEAN,
},
bbcNews: {
type: Sequelize.BOOLEAN,
},
},
{
tableName: 'user_sources',
},
);
UserSources.belongsTo(User);
// User.UserSources = User.hasOne(UserSources);
// User.hasOne(UserSources);
(async function test() {
try {
await sequelize.sync({ force: true });
// 1. User.UserSources = User.hasOne(UserSources);
// await User.create(
// {
// email: 'example@gmail.com',
// password: '123',
// user_source: {
// abcNews: true,
// bbcNews: true,
// },
// },
// {
// include: [
// {
// association: User.UserSources,
// },
// ],
// },
// );
// 2. User.hasOne(UserSources);
// await User.create(
// {
// email: 'example@gmail.com',
// password: '123',
// user_source: {
// abcNews: true,
// bbcNews: true,
// },
// },
// {
// include: [UserSources],
// },
// );
// 3. UserSources.belongsTo(User);
await UserSources.create(
{
abcNews: true,
bbcNews: true,
user: {
email: 'example@gmail.com',
password: '123',
},
},
{
include: [User],
},
);
} catch (error) {
console.log(error);
} finally {
await sequelize.close();
}
})();
执行上述代码后,查看数据库中的数据记录:
node-sequelize-examples=# select * from "users";
id | email | password
----+-------------------+----------
1 | example@gmail.com | 123
(1 row)
node-sequelize-examples=# select * from "user_sources";
id | abcNews | bbcNews | userId
----+---------+---------+--------
1 | t | t | 1
(1 row)
数据记录按预期创建。
推荐阅读
- javascript - 如何在材质 UI 表中创建子标题
- python - 为什么可变数据类型生成的哈希码不能成为python中的字典键?
- scheduled-tasks - 无法安排录音功能
- flutter - 升级到 Flutter 到 2.5.0 后,无法从 Android Studio 的 iOS 模拟器运行应用程序
- redis - 如何创建没有副本的redis集群
- python-3.x - 二维列表操作
- java - 我正在尝试创建许多与从文件中读取的行数相同的 Employee 对象
- c# - 如何从 C# WPF 中的嵌入字体将字体文件添加到 Stimulsoft 报告
- reactjs - 当我通过身份验证包装组件时,Nextjs 未呈现服务器端
- java - 无法从 url 加载 PDF