PostgreSQL的错误:关系已存在 - 在创建表的外键

问题描述 投票:0回答:2

我想提出一个表,如下所示:

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
    );

我不假设索引名必须是表之间的独特之处?

database postgresql indexing namespaces
2个回答
2
投票

索引住在同一个命名空间表,视图和序列号,以便在一个模式中,你不能对任何对象使用相同的名称两次。

要么选择一个不同的名称,或有PostgreSQL的选择一个给你:

CREATE INDEX ON creator.lists (account_id);

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).
© www.soinside.com 2019 - 2024. All rights reserved.