我创建了一个表来存储用户的休假数据。之后,为多列设置了唯一索引,但在执行插入SQL时,它又重复了数据
这是我的DDL:
create table day_off_movements
(
id bigserial
primary key,
user_id bigint
constraint fk_day_off_movements_user
references users,
proxy_user_id bigint
constraint fk_day_off_movements_proxy_users
references users,
reason text,
day_off_start_date timestamp with time zone,
day_off_end_date timestamp with time zone,
used_days bigint,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
目录:
create unique index idx_day_off_all
on day_off_movements (user_id, proxy_user_id, day_off_start_date, day_off_end_date);
控制查询:
SELECT user_id,proxy_user_id,day_off_end_date,day_off_start_date,count(*)
FROM public.day_off_movements t
GROUP BY user_id, proxy_user_id, day_off_end_date, day_off_start_date
以JSON格式输出:
[
{
"user_id": 961,
"proxy_user_id": null,
"day_off_end_date": "2020-07-29 00:00:00.000000 +00:00",
"day_off_start_date": "2020-07-27 00:00:00.000000 +00:00",
"count": 3
}
]
我的错在哪里? 谢谢你们的帮助。
user_id
,那个索引不会报错吗?只有当所有列都相同时,唯一索引才会报错。 - Laurenz Albe