アレについて記す

GORMの関連設定とDDL

Posted on April 13, 2014 at 10:22 (JST)

GrailsのGORMにて自動生成されるDB設定などについての知識を深めるため、実験してみました。
今回は関連に関する設定について記載します。
DBはPostgresqlを使用し、DDLの出力は$ grails schema-exportにて行っています。

サンプルソースはGithubにて公開しています。[ my-grails-sample ]
Grails公式ドキュメントはこちら (日本語意訳版はこちら

hasOneとbelongsTo、フィールド宣言

No.Domain ADomain BDDL ADDL B
field
宣言
hasOnefield
宣言
belongsTohasOne カラム
(FK)
NOT NULL
制約
一意制約カラム
(FK)
NOT NULL
制約
一意制約
1 - - - - - - - - - - -
2 - - - - - - - -
3 - - - - - - -
4 - - - - - - - -
5 - - - - - - -
6 - - - - - -
7 - - - - Caused by MappingException: hasOne property [A07.b07] is not bidirectional. Specify the other side of the relationship!
8 - - - - - -
9 - - - - -

  • フィールドに関連するドメインを宣言した場合、外部キー(カラム)となる。
  • belongsToを宣言した場合、外部キーとなる。ただし相手側のドメインのフィールドにbelongsToを記載したクラスが宣言されていた場合は、相手側のテーブルに外部キー(カラム)が設備される。
  • No.9のように、実質使えない設定であってもテーブル作成時にはエラーにならない。

hasManyとbelongsTo、フィールド宣言

No.Domain ADomain BDDL ADDL B
field宣言
(Set<B>)
hasManyfield
宣言
belongsTohasOne カラム
(FK)
NOT NULL
制約
一意制約カラム
(FK)
NOT NULL
制約
一意制約
13 - - - - - - - - - -
14 - - - - - - -
15 - - - - - - -
16 - - - - - - - -
17 - - - - - - -
18 - - - - - - -
19 - - - MappingException: hasOne property [B19.a19] is not bidirectional. Specify the other side of the relationship!
20 - - - - - - -

  • No.14,15,17,18,20の組み合わせにより生成されるDDLは同一であった。GORMを扱う上での挙動(カスケード処理)などに差異があるようだが、詳細は未確認。
  • No.16の★は中間関連テーブルが作成されたことを示している。

hasManyとフィールド宣言

No.Domain ADomain BDDL ADDL B
field宣言
(Set<B>)
hasManyfield宣言
(Set<A>)
hasMany カラム
(FK)
NOT NULL
制約
一意制約カラム
(FK)
NOT NULL
制約
一意制約
21 - - - - - - - -
22 - - - - - -
23 - - No owner defined between domain classes [class sample.ddl.A23] and [class sample.ddl.B23] in a many-to-many relationship. Example: static belongsTo = sample.ddl.B23

  • No.22の★は中間関連テーブルが作成されたことを示している。

DDL

生成したDDLは以下になります。

[ ddl.sql(一部抜粋)]

create table a01 (id int8 not null, version int8 not null, primary key (id));
create table a02 (id int8 not null, version int8 not null, b02_id int8 not null, primary key (id));
create table a03 (id int8 not null, version int8 not null, b03_id int8, primary key (id));
create table a04 (id int8 not null, version int8 not null, primary key (id));
create table a05 (id int8 not null, version int8 not null, b05_id int8 not null, primary key (id));
create table a06 (id int8 not null, version int8 not null, primary key (id));
create table a08 (id int8 not null, version int8 not null, primary key (id));
create table a09 (id int8 not null, version int8 not null, primary key (id));
create table a10 (id int8 not null, version int8 not null, b10_id int8 not null, primary key (id));
create table a13 (id int8 not null, version int8 not null, primary key (id));
create table a14 (id int8 not null, version int8 not null, primary key (id));
create table a15 (id int8 not null, version int8 not null, primary key (id));
create table a16 (id int8 not null, version int8 not null, primary key (id));
create table a16_b16 (a16_b16s_id int8, b16_id int8);
create table a17 (id int8 not null, version int8 not null, primary key (id));
create table a18 (id int8 not null, version int8 not null, primary key (id));
create table a20 (id int8 not null, version int8 not null, primary key (id));
create table a21 (id int8 not null, version int8 not null, primary key (id));
create table a22 (id int8 not null, version int8 not null, primary key (id));
create table b01 (id int8 not null, version int8 not null, primary key (id));
create table b02 (id int8 not null, version int8 not null, primary key (id));
create table b03 (id int8 not null, version int8 not null, a03_id int8, primary key (id));
create table b04 (id int8 not null, version int8 not null, a04_id int8 not null, primary key (id));
create table b05 (id int8 not null, version int8 not null, primary key (id));
create table b06 (id int8 not null, version int8 not null, a06_id int8 not null unique, primary key (id));
create table b08 (id int8 not null, version int8 not null, a08_id int8 not null unique, primary key (id));
create table b09 (id int8 not null, version int8 not null, a09_id int8 not null unique, primary key (id));
create table b10 (id int8 not null, version int8 not null, a10_id int8 not null, primary key (id));
create table b13 (id int8 not null, version int8 not null, primary key (id));
create table b14 (id int8 not null, version int8 not null, a14_id int8 not null, primary key (id));
create table b15 (id int8 not null, version int8 not null, a15_id int8 not null, primary key (id));
create table b16 (id int8 not null, version int8 not null, primary key (id));
create table b17 (id int8 not null, version int8 not null, a17_id int8 not null, primary key (id));
create table b18 (id int8 not null, version int8 not null, a18_id int8 not null, primary key (id));
create table b20 (id int8 not null, version int8 not null, a20_id int8 not null, primary key (id));
create table b21 (id int8 not null, version int8 not null, primary key (id));
create table b22 (id int8 not null, version int8 not null, primary key (id));
create table b22_a22 (b22_a32s_id int8, a22_id int8);
alter table a02 add constraint FK172237DFD51D4 foreign key (b02_id) references b02;
alter table a03 add constraint FK172247DFDC634 foreign key (b03_id) references b03;
alter table a05 add constraint FK172267DFEAEF4 foreign key (b05_id) references b05;
alter table a10 add constraint FK17240D405DDB6 foreign key (id) references b10;
alter table a16_b16 add constraint FK61FF848E6AAA12D5 foreign key (a16_b16s_id) references a16;
alter table a16_b16 add constraint FK61FF848E7E0D3AF4 foreign key (b16_id) references b16;
alter table b03 add constraint FK175E57C48E9D4 foreign key (a03_id) references a03;
alter table b04 add constraint FK175E67C495E34 foreign key (a04_id) references a04;
alter table b06 add constraint FK175E87C4A46F4 foreign key (a06_id) references a06;
alter table b08 add constraint FK175EA7C4B2FB4 foreign key (a08_id) references a08;
alter table b09 add constraint FK175EB7C4BA414 foreign key (a09_id) references a09;
alter table b10 add constraint FK17601D405D9F5 foreign key (id) references a10;
alter table b14 add constraint FK176057C5775D4 foreign key (a14_id) references a14;
alter table b15 add constraint FK176067C57EA34 foreign key (a15_id) references a15;
alter table b17 add constraint FK176087C58D2F4 foreign key (a17_id) references a17;
alter table b18 add constraint FK176097C594754 foreign key (a18_id) references a18;
alter table b20 add constraint FK176207C63BBF4 foreign key (a20_id) references a20;
alter table b22_a22 add constraint FK986236C471119686 foreign key (b22_a32s_id) references b22;
alter table b22_a22 add constraint FK986236C47C64A4B4 foreign key (a22_id) references a22;
create sequence hibernate_sequence;

今回はGORMの設定とDDLに注視し、パターンを網羅することを目的としました。
実際に使える組み合わせは、記載したパターンの中のごく一部になると思います。