1

I have a strange error in Postgres during creation of sequence and linking on existing table.

I tried to execute the following script on existing DB which have a schema named 'testschema':

BEGIN TRANSACTION;

CREATE TABLE testschema.mytable (
    id INTEGER,
    value VARCHAR(30),
    CONSTRAINT pk_mytable PRIMARY KEY (id));

CREATE SEQUENCE testschema.mytable_id_seq;
ALTER TABLE testschema.mytable ALTER COLUMN  id SET NOT NULL;
ALTER TABLE testschema.mytable ALTER COLUMN  id SET DEFAULT nextval('mytable_id_seq');

COMMIT;

I got the following error for the second ALTER TABLE query:

********** Erreur **********

ERREUR: la relation « mytable_id_seq » n'existe pas État SQL :42P01

Error message can be translated by "Relation 'mytable_id_seq' does not exist".

If I try to execute this script without schema (i.e. in the public schema), it works.

I don't understand what it is failing. Does anyone see where the problem is?

Thanks for your help.

1 Answer 1

2

Replace:

ALTER TABLE testschema.mytable ALTER COLUMN  id SET DEFAULT nextval('mytable_id_seq');

by

ALTER TABLE testschema.mytable ALTER COLUMN  id SET DEFAULT nextval('testschema.mytable_id_seq');

By default, objects without using schema quallifier are searched in the schema that are in the search_path variable. by default default, only public schema exists in this variable.

So the simplest way is to use full quallified qualifier when referencing your objects. Or, you can put your new schema in the path, like this:

SET search_path TO testschema,public;

Source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.