I have a table, foo
. For the purposes of a quick upgrade/deploy of my site, I made a new table, tmp_foo
, to contain some new data, by doing:
create table tmp_foo (like foo including constraints including defaults including indexes);
Now each table has a PK id
column that looks like:
Column | Type | Modifiers
-------------+-----------------------+--------------------------------------------------------------------------
id | integer | not null default nextval('foo_id_seq'::regclass)
The important point is that both tables rely on the exact same sequence, foo_id_seq
. There is no tmp_foo_id_seq
. This seems OK for my purposes.
After this, I loaded tmp_foo
with new data and renamed the tables so that tmp_foo
took over as the real foo
, and the original foo
became foo_old
. Now I try to drop foo_old
:
db=> drop table foo_old ;
ERROR: cannot drop table foo_old because other objects depend on it
DETAIL: default for table foo_old column id depends on sequence foo_id_seq
Fair enough, the id
column default still depends on the sequence.
db=> alter table foo_old alter column id drop default;
Here's the kicker.
db=> 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
So foo_old
no longer has any visible dependency on the sequence, yet it still tries to drop the sequence along with the table (and obviously won't because the new table depends upon it).
So the question is two-part:
(On PostgreSQL 8.4)
PostgreSQL has a DROP TABLE statement that is used to remove an existing table or tables from the database. Syntax: DROP TABLE [IF EXISTS] table_name [CASCADE | RESTRICT]; Let's analyze the above syntax: We specify the table name after the DROP TABLE keyword to remove the table permanently from the database.
Notes. DROP SEQUENCE only removes sequences, not tables. However, DROP TABLE can remove both sequences and tables.
The CASCADE option allows you to remove the table and its dependent objects. The RESTRICT option rejects the removal if there is any object depends on the table. The RESTRICT option is the default if you don't explicitly specify it in the DROP TABLE statement.
DROP TABLE removes tables from the database. Only the table owner, the schema owner, and superuser can drop a table. To empty a table of rows without destroying the table, use DELETE or TRUNCATE . DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table.
Try this:
ALTER SEQUENCE foo_id_seq OWNED BY NONE
then you should be able to drop the table.
To retrieve the "owner" of a sequence use the following query
SELECT s.relname as sequence_name, n.nspname as sequence_schema, t.relname as related_table, a.attname as related_column FROM pg_class s, pg_depend d, pg_class t, pg_attribute a, pg_namespace n WHERE s.relkind = 'S' AND n.oid = s.relnamespace AND d.objid = s.oid AND d.refobjid = t.oid AND (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With