[pgsql-jp: 39345] 外部キーにおけるデータ型の相違

EBIHARA, Yuichiro ebihara @ iplocks.co.jp
2008年 4月 21日 (月) 20:54:50 JST


参照性合成制約とデータ型に関する質問です。

下記のようにchildテーブルのparent_id列を、parentテーブルの主キー(id列)に対する外部キーとして定義します。

create table parent (id integer primary key);
create table child (id integer primary key, parent_id decimal(10, 0));
alter table child add constraint child_fk foreign key(parent_id)
references parent(id);

parentテーブルのid列はINTEGER型、それを参照するparent_id列はDECIMAL(10, 0)型と異なります。

8.1では以下のようにWARNINGが出ながらも外部キーを作成できていました。

WARNING:  foreign key constraint "child_fk" will require costly sequential scans
DETAIL:  Key columns "parent_id" and "id" are of different types:
numeric and integer.
ALTER TABLE

8.3では以下の通りエラーになります。

ERROR:  foreign key constraint "child_fk" cannot be implemented
DETAIL:  Key columns "parent_id" and "id" are of incompatible types:
numeric and integer.

これは8.3における仕様変更の影響でしょうか?
8.3のリリースノートの以下の記述が当てはまっているように見えますが。

「外部キーはデータ型をまたがる参照においてインデックス付け可能な状態に一致しなければならなくなりました。(Tom)
これは意味的な一貫性を向上させ、また、性能に関する問題を防止する役に立ちます。 」

ご存知の方、教えていただけると助かります。

-- 
海老原 雄一郎 / EBIHARA, Yuichiro
  Email: ebihara @ iplocks.co.jp



pgsql-jp メーリングリストの案内