【发布时间】: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