我正在制作一张桌子,如下所示:
CREATE TABLE creator.lists
(
_id bigserial PRIMARY KEY NOT NULL,
account_id bigint NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
display_name text DEFAULT '',
name text DEFAULT '',
extra jsonb,
FOREIGN KEY (account_id)
REFERENCES creator.accounts (_id)
ON DELETE CASCADE
);
但我收到此错误:
ERROR: relation "account_id_index" already exists
当我跑步时:
CREATE INDEX
account_id_index
ON
creator.lists
(
account_id
);
如何在外键上创建索引?我正在运行v11.1
请注意,我之前也对另一个表运行过类似的命令:
CREATE INDEX
account_id_index
ON
creator.contacts
(
account_id
);
我不认为表之间的索引名称必须唯一吗?
答案 0 :(得分:1)
索引与表,视图和序列位于同一命名空间中,因此您不能在一个模式中对任何这些对象使用相同的名称两次。
要么选择其他名称,要么让PostgreSQL为您选择一个名称:
CREATE INDEX ON creator.lists (account_id);
答案 1 :(得分:0)
好吧,似乎索引名称必须是唯一的,因为删除了固定的命名方式:
CREATE INDEX
ON
creator.contacts
(
account_id
);
来自docs:
name
The name of the index to be created. No schema name can be included here; the index is always created in the same schema as its parent table. If the name is omitted, PostgreSQL chooses a suitable name based on the parent table's name and the indexed column name(s).