Suppose you have 2 tables, namely foo
and foo_old
, each having a column called "id"
.
Suppose both tables have the following schema:
id integer NOT NULL DEFAULT nextval('foo_id_seq'::regclass)
and you want to DROP the foo_old
table:
test=# DROP TABLE foo_old;
ERROR: cannot drop table foo_old because other objects depend on it
DETAIL: default for table foo column id depends on sequence foo_id_seq
HINT: Use DROP ... CASCADE to drop the dependent objects too.
You do not want to drop the sequence as the other table, foo
, relies on it. Therefore, CASCADE
is not an option.
The problem here is that the sequence has a dependency with the other table / column.
You can check who owns the sequence with the \d+
meta-command from psql
:
test=*# \d+ foo_id_seq
Sequence "public.foo_id_seq"
Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
bigint | 1 | 1 | 9223372036854775807 | 1 | no | 1
Owned by: public.foo_old.id
test=*#
You can alter the sequence so it is owned by the other table:
test=*# ALTER SEQUENCE public.foo_id_seq OWNED BY public.foo.id;
ALTER SEQUENCE
Then, re-check the sequence:
test=*# \d+ foo_id_seq
Sequence "public.foo_id_seq"
Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
bigint | 1 | 1 | 9223372036854775807 | 1 | no | 1
Owned by: public.foo.id
Now you can DROP
the foo_old
table:
test=*# DROP TABLE foo_old;
DROP TABLE
test=*#
The sequence has a dependency with a column of the table that you want to drop.