小编ics*_*kal的帖子

为什么我的唯一索引在 Postgresql 中不起作用?

我创建了一个表来存储用户的休息日数据。之后,为多列设置唯一索引,但是当执行插入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
);
Run Code Online (Sandbox Code Playgroud)

指数:

create unique index idx_day_off_all
    on day_off_movements (user_id, proxy_user_id, day_off_start_date, day_off_end_date);
Run Code Online (Sandbox Code Playgroud)

控制查询:

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
Run Code Online (Sandbox Code Playgroud)

输出为 json:

[
  {
    "user_id": 961,
    "proxy_user_id": null,
    "day_off_end_date": …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-15

3
推荐指数
2
解决办法
170
查看次数

标签 统计

postgresql ×1

postgresql-15 ×1