0

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.

Irfandy Jip
  • 1,308
  • 1
  • 18
  • 36
  • I'm not sure on how to set this as a duplicate, but the answer is here, [Ensure unique field value in loopback model](https://stackoverflow.com/questions/25927961/ensure-unique-field-value-in-loopback-model?rq=1) – Irfandy Jip Jul 02 '21 at 03:19

1 Answers1

0

@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

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 17:41