Search code examples
node.jspostgresqlnode-postgres

Postgres relation does not exist when adding column to table?


I'm trying to create a schema and add tables to it via node using node-postgres (pg). The basic order of events is as follows:

1. Create schema
2. Create table in schema
3. Create columns in table in schema

I can verify that the schema and the table are being created without issues, but I get a relation does not exist error when trying to add the first column to the table. The query string for creating the column looks like this:

"ALTER TABLE " +
schemaName +
".process ADD COLUMN process_id bigint NOT NULL DEFAULT nextval('" +
schemaName +
".process_process_id_seq'::regclass)";

I've confirmed in the console log that the schema name variable matches what is was used to successfully create the table. Any ideas on why the error is being thrown here?


Solution

  • The sequence needs to be created beforehand.

    But you could also use the bigserial type which is a shortcut for a bigint column with a sequence and a respective DEFAULT created automatically. Something along the lines of:

    "ALTER TABLE " +
    schemaName +
    ".process ADD COLUMN process_id bigserial";
    

    Maybe you fell over that. Later, when the table/column was created you cannot see the ...serial anymore but the actual type and the DEFAULT.

    More on ...serial types can be found in the documentation.