如何在环回4中使模型在其架构中具有唯一值? [英] How to make a model in loopback 4 to have unique values in its schema?
问题描述
我正在尝试找到一种方法来具有与我放进去时一样的功能 唯一:在表达模式中为true.我该如何在回送4中完成此操作.我尝试在属性装饰器中放入unique true,但是它不起作用.
I am trying to find a way to have the same funtionality that happens when I put unique:true in express schema. How do I accomplish this in loopback 4. I tried putting unique true in the property decorator but it did not work.
@property({
type: 'string',
id: true,
required: false,
unique: true,
})
id: string;
这不起作用
推荐答案
@property
装饰器借用了与LB3中相同的属性.假设我已经了解您的要求,则可以使用index
属性在整个集合中强制字段的唯一性.
对于"id"之类的字段,属性装饰器将采用以下参数:
@property
decorator in LB4 borrows the same properties as in LB3. Assuming that I have understood your requirements, you can make use of the index
property to enforce the uniqueness of a field across the collection.
For a field like 'id', the property decorator would take the following arguments:
@property({
type: 'string',
id: true,
required: false,
index: {
unique: true
}
})
id: string;
此外,如果您使用的是MongoDB生成的"id",则无需显式实施唯一性,但以上内容适用于其他字段,例如电子邮件,用户名等.
Moreover, if you are using the 'id' generated by MongoDB, you do not need to enforce uniqueness explicitly but the above should work for other fields like email, username etc.
这篇关于如何在环回4中使模型在其架构中具有唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!