Re: Does dropping a column from a table mess up foreign keys?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "IRWIN,KEITH (Non-HP-Corvallis,ex1)" <keith_irwin(at)non(dot)hp(dot)com>
Cc: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Does dropping a column from a table mess up foreign keys?
Date: 2001-07-20 23:07:07
Message-ID: Pine.BSF.4.21.0107201602020.42285-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nathan Myers 2001-07-20 23:20:53 Re: Re: RPM source files should be in CVS (was Re: [GENERAL] psql -l)
Previous Message Trond Eivind =?iso-8859-1?q?Glomsr=F8d?= 2001-07-20 23:05:46 Re: Re: RPM source files should be in CVS (was Re: [GENERAL] psql -l)