PostgreSQL Error: Relation already exists

后端 未结 8 1829
暖寄归人
暖寄归人 2020-12-05 12:37

I am trying to create a table that was dropped previously.

But when I do the CREATE TABLE A ... I am getting below error:

Rela

8条回答
  •  清歌不尽
    2020-12-05 13:32

    You cannot create a table with a name that is identical to an existing table or view in the cluster. To modify an existing table, use ALTER TABLE (link), or to drop all data currently in the table and create an empty table with the desired schema, issue DROP TABLE before CREATE TABLE.

    It could be that the sequence you are creating is the culprit. In PostgreSQL, sequences are implemented as a table with a particular set of columns. If you already have the sequence defined, you should probably skip creating it. Unfortunately, there's no equivalent in CREATE SEQUENCE to the IF NOT EXISTS construct available in CREATE TABLE. By the looks of it, you might be creating your schema unconditionally, anyways, so it's reasonable to use

    DROP TABLE IF EXISTS csd_relationship;
    DROP SEQUENCE IF EXISTS csd_relationship_csd_relationship_id_seq;
    

    before the rest of your schema update; In case it isn't obvious, This will delete all of the data in the csd_relationship table, if there is any

提交回复
热议问题