Re: Does dropping a column from a table mess up foreign keys? - Mailing list pgsql-general

From Stephan Szabo
Subject Re: Does dropping a column from a table mess up foreign keys?
Date
Msg-id Pine.BSF.4.21.0107201602020.42285-100000@megazone23.bigpanda.com
Whole thread Raw
In response to Does dropping a column from a table mess up foreign keys?  ("IRWIN,KEITH (Non-HP-Corvallis,ex1)" <keith_irwin@non.hp.com>)
List pgsql-general
On Fri, 20 Jul 2001, IRWIN,KEITH (Non-HP-Corvallis,ex1) wrote:

> Hi--
>
> I'm getting the following error:
>
>     ERROR:  Relation "accounts" with OID 72496 no longer exists
>
> What I did was to drop a couple of columns using the example Bruce
> provides in his book on page 264.  Briefly, it's something like:
>
>     create table temp as select * from accounts;
>     drop table accounts;
>     create table accounts (etc with columns missing);
>     insert into accounts select <all except dropped cols> from temp;
>     drop table temp;
>     grant update,select,insert,delete on accounts to user;

If you've dumped and restored with 7.0's pg_dump (I'm not sure when it was
fixed, may have been in 7.1.2) there was a problem with the dumped trigger
statements which caused the relationship that tells when to drop the
triggers for fk to not exist after the restore.  Theoretically, your
constraints should have gone away at the drop table accounts;.  I.e.,
even in the best case, the above will not preserve foreign key constraints
pointing to the changed table, you'd need to use alter table to re-add
the constraints.

> And so on.  I've also updated an "accounts_view" based on this table so
> that the dropped columns wouldn't appear (the view being defined with an
> asterisk field list).
>
> I have lots of tables with "references accounts(id)" in them.  Are these
> going to be screwed up because of the drop/create above?
>
> I even tried dumping the DB after the changes, then pg_restoring them, but
> I get a message something like, Relation ACCOUNTS doesn't exist.  I was
> thinking that restoring the db in this way would recalculate the OIDs.

When do you get the relation ACCOUNTS doesn't exist message?  When you try
to do an insert/update?

I'd suggest starting by looking pg_trigger and dropping the constraint
triggers (warning, you need to double quote the constraint name, the
case is significant) that reference accounts and use alter table add
constraint to add the constraints back.



pgsql-general by date:

Previous
From: "Dr. Evil"
Date:
Subject: Changes to C interface from 7.0 to 7.1
Next
From: teg@redhat.com (Trond Eivind Glomsrød)
Date:
Subject: Re: RPM source files should be in CVS (was Re: psql -l)