我是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;
答案 0 :(得分:0)
我认为这些会起作用。将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"
}
}
}