sql - Deleting a table in PostgreSQL without deleting an associated sequence -
i have table, foo. purposes of quick upgrade/deploy of site, made new table, tmp_foo, contain new data, doing:
create table tmp_foo (like foo including constraints including defaults including indexes); now each table has pk id column looks like:
   column    |         type          |                                modifiers                                  -------------+-----------------------+--------------------------------------------------------------------------  id          | integer               | not null default nextval('foo_id_seq'::regclass) the important point both tables rely on exact same sequence, foo_id_seq. there no tmp_foo_id_seq. seems ok purposes.
after this, loaded tmp_foo new data , renamed tables tmp_foo took on real foo, , original foo became foo_old. try drop foo_old:
db=> drop table foo_old ; error:  cannot drop table foo_old because other objects depend on detail:  default table foo_old column id depends on sequence foo_id_seq fair enough, id column default still depends on sequence.
db=> alter table foo_old alter column id drop default; here's kicker.
db=> drop table foo_old ; error:  cannot drop table foo_old because other objects depend on detail:  default table foo column id depends on sequence foo_id_seq so foo_old no longer has visible dependency on sequence, yet still tries drop sequence along table (and won't because new table depends upon it).
so question two-part:
- why sequence still linked old table?
- is there way around doesn't involve making new table depend on new or different sequence (if help)?
(on postgresql 8.4)
try this:
alter sequence foo_id_seq owned none
then should able drop table.
to retrieve "owner" of sequence use following query
select s.relname sequence_name, n.nspname sequence_schema, t.relname related_table, a.attname related_column pg_class s, pg_depend d, pg_class t, pg_attribute a, pg_namespace n s.relkind = 's' , n.oid = s.relnamespace , d.objid = s.oid , d.refobjid = t.oid , (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
Comments
Post a Comment