dbTalk Databases Forums  

[BUGS] Odd Foreign Key Bug

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


Discuss [BUGS] Odd Foreign Key Bug in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ara Anjargolian
 
Posts: n/a

Default [BUGS] Odd Foreign Key Bug - 10-07-2003 , 01:18 PM






I'm using PostgreSQL 7.3.4 on cygwin

A coding error on my part revealed what appears
to be a a bug in PostgreSQL's foreign key behavior.

reduced bug-revealing code:
******************************
create table languages(
language_code char(2) not null,
language varchar(100) not null,
constraint languages_pk primary key (language_code)
);

create table content (
content_id integer not null,
language_code integer not null,
body text,
constraint content_pk primary key (content_id)
);

alter table content
add constraint languages_content_fk foreign key (language_code) references
languages on delete restrict on update cascade;
****************
Now, after these definitions run:

insert into languages values ('AA', 'whocares');

Then do:

update languages set language_code = lower(language_code);

And you get the error:

ERROR: column "language_code" is of type integer but expression is of type
character
You will need to rewrite or cast the expression

PostgreSQL seems to think that language_code is an integer
because of an errant foreign key constraint.

Note that language_code integer in the content table references
language_code char(2) in the languages table (This was a mistake by me that
revealed this odd behavior).

Also notice that the foreign key constraint is not declared until after the
table
definition. This error does not show up if the foreign key is added when
the content
table is defined.

I don't know if this is a bug or expected behavior for non-sensical SQL
code, but,
at the very least, PostgreSQL displays different behavior depending on when
you
declare the foreign key constraint, which seems strange.

Regards,
Ara Anjargolian





---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)

Reply With Quote
  #2  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: [BUGS] Odd Foreign Key Bug - 10-07-2003 , 01:41 PM







On Mon, 6 Oct 2003, Ara Anjargolian wrote:
Quote:
Also notice that the foreign key constraint is not declared until after
the table definition. This error does not show up if the foreign key is
added when the content table is defined.
It works the same for me as a table constraint or as an alter as long as
on update cascade is given in both cases. It'd be nice to fail at
constraint creation time if on update cascade is given and the type on the
primary key table wasn't assignment convertable to the type on the foreign
key table.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


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.