gpt4 book ai didi

node.js - 我无法在环回 ORM 模型中设置唯一字段

转载 作者:行者123 更新时间:2023-11-29 12:23:31 24 4
gpt4 key购买 nike

我是 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;

最佳答案

我认为这些会起作用。要么将 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"
}
}
}

关于node.js - 我无法在环回 ORM 模型中设置唯一字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55971665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com