'How to add a unique constraints through Loopback 4 Model?
I have a Department model in my project. The properties are id, name. name of course should be unique. However I can't seem to look where the setting is so I can put something like
@model()
export class Department extends Entity {
@property({
type: 'number',
id: true,
generated: true,
})
id?: number;
@property({
type: 'string',
required: true,
unique: true // ---> This is what I want. But how?
})
name: string;
constructor(data?: Partial<Department>) {
super(data);
}
}
I tried digging to Model documentation, it seems that there is something I can do with the @model decorator. However I found no documentation about it.
Also, I want to do this in PostgreSQL as a datasource. Any clue? Any advice would be appreciated.
Solution 1:[1]
@property({ type: 'string', required: true, index: { unique: true, } }) name: string;
Using index object.
Migrate Database - Modify or alter table
run command : npm run build migrate database : npm run migrate
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sharma Ashish |
