簡體   English   中英

如何通過 Loopback 4 Model 添加唯一約束?

[英]How to add a unique constraints through Loopback 4 Model?

我的項目中有一個Department model。 屬性是idname name當然應該是唯一的。 但是我似乎看不到設置在哪里,所以我可以放一些類似的東西

@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);
  }
}

我嘗試挖掘Model文檔,似乎我可以使用@model裝飾器做一些事情。 但是我沒有找到關於它的文檔。

此外,我想在 PostgreSQL 作為數據源執行此操作。 有什么線索嗎? 任何意見,將不勝感激。

@property({ type: 'string', required: true, index: { unique: true, } }) name: string;

使用索引 object。

遷移數據庫 - 修改或更改表

運行命令: npm 運行構建遷移數據庫: npm 運行遷移

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM