我可以在 Loopback4 的 hasManyThrough (M:M) table 上创建唯一约束吗
Can I create a unique constraint on Loopback4's hasManyThrough (M:M) table
前言
我对 Loopback 4 有点陌生,我已经成功创建了 2 个工作的 CRUD 控制器、存储库、模型等。我有 Users 和 Organizations ,并且我已经使用 relation generator. So, I can create a user, create an organization, and separately I can link them together in a many-to-many table called "membership". All of this follows (as far as I know) loopback best practices per their docs 在 Users 和 Organizations 之间成功创建了 M:M 关系.
问题:
如何确保会员中的每条记录 table 都是唯一的?
here's an example of my table with duplicates
例如,用户 #1 Jeff 加入组织 #1,Amazon。一些错误代码然后第二次尝试将 Jeff 添加到 Amazon,我希望它失败。
我想我可以简单地检查数据库是否存在记录,但是将此约束添加到模型似乎更清晰,这样如果重复,任何尝试的插入都会失败。我在文档中四处寻找,但没有找到任何东西。
感谢任何帮助!
在数据库设计中,唯一约束将由联结 table 上的 compound/composite 键强制执行。这将强制执行一对唯一的 orgId
和 userId
.
因此,交汇点 table 应该只有 2 列:
- 组织编号
- 用户ID
要在 LoopBack 4 中创建复合键,请按如下方式更新直通模型:
// Some parts were omitted for bevity
@model()
export class OrgUser extends Entity {
@property({id: 1})
orgId: number;
@property({id: 2})
userId: number;
constructor(data?: Partial<OrgUser>) {
super(data);
}
}
前言
我对 Loopback 4 有点陌生,我已经成功创建了 2 个工作的 CRUD 控制器、存储库、模型等。我有 Users 和 Organizations ,并且我已经使用 relation generator. So, I can create a user, create an organization, and separately I can link them together in a many-to-many table called "membership". All of this follows (as far as I know) loopback best practices per their docs 在 Users 和 Organizations 之间成功创建了 M:M 关系.
问题:
如何确保会员中的每条记录 table 都是唯一的?
here's an example of my table with duplicates
例如,用户 #1 Jeff 加入组织 #1,Amazon。一些错误代码然后第二次尝试将 Jeff 添加到 Amazon,我希望它失败。
我想我可以简单地检查数据库是否存在记录,但是将此约束添加到模型似乎更清晰,这样如果重复,任何尝试的插入都会失败。我在文档中四处寻找,但没有找到任何东西。
感谢任何帮助!
在数据库设计中,唯一约束将由联结 table 上的 compound/composite 键强制执行。这将强制执行一对唯一的 orgId
和 userId
.
因此,交汇点 table 应该只有 2 列:
- 组织编号
- 用户ID
要在 LoopBack 4 中创建复合键,请按如下方式更新直通模型:
// Some parts were omitted for bevity
@model()
export class OrgUser extends Entity {
@property({id: 1})
orgId: number;
@property({id: 2})
userId: number;
constructor(data?: Partial<OrgUser>) {
super(data);
}
}