PostgreSQL Error: Relation already exists
I finally discover the error. The problem is that the primary key constraint name is equal the table name. I don know how postgres represents constraints, but I think the error "Relation already exists" was being triggered during the creation of the primary key constraint because the table was already declared. But because of this error, the table wasnt created at the end.
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
There should be no single quotes here . Single quotes are for string literals: 'A'
'some value'
.
Either use double quotes to preserve the upper case spelling of "A":
CREATE TABLE "A" ...
Or don't use quotes at all:
CREATE TABLE A ...
which is identical to
CREATE TABLE a ...
because all unquoted identifiers are folded to lower case automatically in PostgreSQL.
You can avoid problems with the index name completely by using simpler syntax:
CREATE TABLE csd_relationship (
csd_relationship_id serial PRIMARY KEY,
type_id integer NOT NULL,
object_id integer NOT NULL
);
Does the same as your original query, only it avoids naming conflicts by picking the next free identifier automatically. More about the serial
type in the manual.