【问题标题】:I can't set unique field in Loopback ORM model我无法在 Loopback ORM 模型中设置唯一字段
【发布时间】:2019-09-22 02:38:19
【问题描述】:

我是 Loopback 3 的新手。 我需要用唯一字段定义模型。 电子邮件字段应该是唯一的。我使用 Postgresql 作为数据库。

我尝试添加 "unique": true 选项。此外,我尝试遵循以下建议:Ensure unique field value in loopback model。但它没有给出预期的结果。

 "properties": {
    "id": {
      "type": "number",
      "id": true,
      "generated": true,
      "postgresql": {
        "dataType": "bigint"
      }
    },
    "name": {
      "type": "string",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "email": {
      "type": "varchar",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "added_date": {
      "type": "date",
      "postgresql": {
        "dataType": "date"
      }
    }
  }

最终,我希望在 Postgres 方案中有一个独特的字段。 在 Postgres 中应该是这样的:

-- Table: public."user"

-- DROP TABLE public."user";

CREATE TABLE public."user"
(
    id bigint NOT NULL DEFAULT nextval('user_id_seq'::regclass),
    name character varying COLLATE pg_catalog."default",
    email character varying COLLATE pg_catalog."default",
    added_date date,
    CONSTRAINT user_pkey PRIMARY KEY (id),
    CONSTRAINT user_email_key UNIQUE (email)

)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public."user"
    OWNER to postgres;

【问题讨论】:

    标签: node.js postgresql orm loopbackjs unique-constraint


    【解决方案1】:

    我认为这些会起作用。将index 属性添加到列def,或者将其添加到模型的索引部分。奇怪的是没有更多的文档。

     "properties": {
        "id": {
          "type": "number",
          "id": true,
          "generated": true,
          "postgresql": {
            "dataType": "bigint"
          }
        },
        "name": {
          "type": "string",
          "postgresql": {
            "dataType": "character varying"
          }
        },
        "email": {
          "type": "varchar",
          "postgresql": {
            "dataType": "character varying"
          }
        },
        "added_date": {
          "type": "date",
          "postgresql": {
            "dataType": "date"
          }
        }
      },
      "indexes": {
        "EMAIL_INDEX": {
          "columns": "email",
          "kind": "unique"
        }
      }
    

    或者

     "properties": {
        "id": {
          "type": "number",
          "id": true,
          "generated": true,
          "postgresql": {
            "dataType": "bigint"
          }
        },
        "name": {
          "type": "string",
          "postgresql": {
            "dataType": "character varying"
          }
        },
        "email": {
          "type": "varchar",
          "postgresql": {
            "dataType": "character varying"
          },
          "index": {"kind": "UNIQUE"}
        },
        "added_date": {
          "type": "date",
          "postgresql": {
            "dataType": "date"
          }
        }
      }
    

    【讨论】:

    • 感谢您的回答,但它不适用于我的情况。我应该将我的 ORM 模型与使用 UNIQUE CONSTRAINT 而不是 UNIQUE INDEX 的 PostgreSQL 方案集成。作为解决方案,我在autoupdateautomugration 之后创建了UNIQUE CONSTRAINT
    猜你喜欢
    • 2015-03-06
    • 2016-09-06
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 2013-12-20
    • 2019-09-04
    相关资源
    最近更新 更多