dbTalk Databases Forums  

[BUGS] Problems renaming referencing column

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] Problems renaming referencing column in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Alexander M. Pravking
 
Posts: n/a

Default [BUGS] Problems renaming referencing column - 07-18-2004 , 04:12 PM






In 7.4.3, if I rename a column which references another table,
constraint trigger fails on update or delete from main table.

There are a couple of similar (and about rename table itself) reports
for 7.0, 7.1 (as Tom Lane said, rename table is fixed in 7.2), but I
see no more reports since 2001.

Here's a simple reproducible example:


fduch@~=# CREATE TABLE master (k integer NOT NULL PRIMARY KEY) WITHOUT OIDS;
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "master_pkey" for table "master"
CREATE TABLE
fduch@~=# CREATE TABLE slave (ref integer REFERENCES master (k)) WITHOUT OIDS;
CREATE TABLE
fduch@~=# INSERT INTO master VALUES (1);
INSERT 0 1
fduch@~=# INSERT INTO master VALUES (2);
INSERT 0 1
fduch@~=# DELETE FROM master WHERE k = 1;
DELETE 1
fduch@~=# ALTER TABLE slave RENAME ref TO k;
ALTER TABLE
fduch@~=# UPDATE master SET k = 2 where k = 2;
ERROR: table "slave" does not have column "ref" referenced by constraint "$1"
fduch@~=# DELETE FROM master WHERE k = 2;
ERROR: table "slave" does not have column "ref" referenced by constraint "$1"


However triggers themselves look good after rename:


fduch@~=# \d slave
Table "public.slave"
Column | Type | Modifiers
--------+---------+-----------
k | integer |
Foreign-key constraints:
"$1" FOREIGN KEY (k) REFERENCES master(k)

fduch@~=# SELECT * from pg_trigger WHERE tgrelid = (SELECT oid FROM pg_class WHERE relname = 'slave');
tgrelid | tgname | tgfoid | tgtype | tgenabled | tgisconstraint | tgconstrname | tgconstrrelid | tgdeferrable | tginitdeferred | tgnargs | tgattr | tgargs
---------+----------------------------+--------+--------+-----------+----------------+--------------+---------------+--------------+----------------+---------+--------+----------------------------------------------------
77304 | RI_ConstraintTrigger_77307 | 1644 | 21 | t | t | $1 | 77300 | f | f | 6 | | $1\000slave\000master\000UNSPECIFIED\000k\000k\000
(1 row)

fduch@~=# SELECT * from pg_trigger WHERE tgrelid = (SELECT oid FROM pg_class WHERE relname = 'master');
tgrelid | tgname | tgfoid | tgtype | tgenabled | tgisconstraint | tgconstrname | tgconstrrelid | tgdeferrable | tginitdeferred | tgnargs | tgattr | tgargs
---------+----------------------------+--------+--------+-----------+----------------+--------------+---------------+--------------+----------------+---------+--------+----------------------------------------------------
77300 | RI_ConstraintTrigger_77309 | 1655 | 17 | t | t | $1 | 77304 | f | f | 6 | | $1\000slave\000master\000UNSPECIFIED\000k\000k\000
77300 | RI_ConstraintTrigger_77308 | 1654 | 9 | t | t | $1 | 77304 | f | f | 6 | | $1\000slave\000master\000UNSPECIFIED\000k\000k\000
(2 rows)


The problem goes away after re-creating the foreign key:

fduch@~=# ALTER TABLE slave DROP CONSTRAINT "$1";
ALTER TABLE
fduch@~=# ALTER TABLE slave ADD CONSTRAINT "$1" FOREIGN KEY (k) REFERENCES master(k);
ALTER TABLE
fduch@~=# DELETE FROM master WHERE k = 2;
DELETE 1


--
Fduch M. Pravking

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.