Skip to content

Commit 2cb4f9b

Browse files
committed
Ignore Composite Foreign Keys.
Boiler doesn't support composite FKs -- And this is OK. However if there is one on the database, it should ignore it and generate valid code.
1 parent 8b8a1c5 commit 2cb4f9b

File tree

11 files changed

+867
-9
lines changed

11 files changed

+867
-9
lines changed

drivers/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,22 @@ func ColumnsFromList(list []string, tablename string) []string {
252252
func CombineConfigAndDBForeignKeys(configForeignKeys []ForeignKey, tableName string, dbForeignKeys []ForeignKey) []ForeignKey {
253253
combinedForeignKeys := make([]ForeignKey, 0, len(configForeignKeys)+len(dbForeignKeys))
254254
appearedColumns := make(map[string]bool)
255+
fkNameCount := make(map[string]int)
256+
257+
// Detect Composite Foreign Keys in the database by counting how many times they appear.
258+
// boiler doesn't support Composite FKs and should ignore those
259+
for _, fk := range dbForeignKeys {
260+
fkNameCount[fk.Name] += 1
261+
}
255262

256263
for _, fk := range configForeignKeys {
257264
// need check table name here cause configForeignKeys contains all foreign keys of all tables
258265
if fk.Table != tableName {
259266
continue
260267
}
268+
if appearedColumns[fk.Column] {
269+
continue
270+
}
261271

262272
combinedForeignKeys = append(combinedForeignKeys, fk)
263273
appearedColumns[fk.Column] = true
@@ -268,7 +278,11 @@ func CombineConfigAndDBForeignKeys(configForeignKeys []ForeignKey, tableName str
268278
if appearedColumns[fk.Column] {
269279
continue
270280
}
281+
if fkNameCount[fk.Name] != 1 {
282+
continue
283+
}
271284
combinedForeignKeys = append(combinedForeignKeys, fk)
285+
appearedColumns[fk.Column] = true
272286
}
273287

274288
return combinedForeignKeys

drivers/sqlboiler-mssql/driver/mssql.golden.json

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,138 @@
11
{
22
"schema": "dbo",
33
"tables": [
4+
{
5+
"name": "node",
6+
"schema_name": "",
7+
"columns": [
8+
{
9+
"name": "id",
10+
"type": "int",
11+
"db_type": "int",
12+
"default": "",
13+
"comment": "",
14+
"nullable": false,
15+
"unique": true,
16+
"validated": false,
17+
"auto_generated": false,
18+
"arr_type": null,
19+
"udt_name": "",
20+
"domain_name": null,
21+
"full_db_type": "int"
22+
},
23+
{
24+
"name": "parent_id",
25+
"type": "null.Int",
26+
"db_type": "int",
27+
"default": "",
28+
"comment": "",
29+
"nullable": true,
30+
"unique": false,
31+
"validated": false,
32+
"auto_generated": false,
33+
"arr_type": null,
34+
"udt_name": "",
35+
"domain_name": null,
36+
"full_db_type": "int"
37+
},
38+
{
39+
"name": "root_id",
40+
"type": "int",
41+
"db_type": "int",
42+
"default": "",
43+
"comment": "",
44+
"nullable": false,
45+
"unique": false,
46+
"validated": false,
47+
"auto_generated": false,
48+
"arr_type": null,
49+
"udt_name": "",
50+
"domain_name": null,
51+
"full_db_type": "int"
52+
}
53+
],
54+
"p_key": {
55+
"name": "PK__node",
56+
"columns": [
57+
"id"
58+
]
59+
},
60+
"f_keys": [
61+
{
62+
"table": "node",
63+
"name": "FK_node_parent",
64+
"column": "parent_id",
65+
"nullable": true,
66+
"unique": false,
67+
"foreign_table": "node",
68+
"foreign_column": "id",
69+
"foreign_column_nullable": false,
70+
"foreign_column_unique": true
71+
},
72+
{
73+
"table": "node",
74+
"name": "FK_node_root",
75+
"column": "root_id",
76+
"nullable": false,
77+
"unique": false,
78+
"foreign_table": "node",
79+
"foreign_column": "id",
80+
"foreign_column_nullable": false,
81+
"foreign_column_unique": true
82+
}
83+
],
84+
"is_join_table": false,
85+
"to_one_relationships": null,
86+
"to_many_relationships": [
87+
{
88+
"name": "FK_node_parent",
89+
"table": "node",
90+
"column": "id",
91+
"nullable": false,
92+
"unique": true,
93+
"foreign_table": "node",
94+
"foreign_column": "parent_id",
95+
"foreign_column_nullable": true,
96+
"foreign_column_unique": false,
97+
"to_join_table": false,
98+
"join_table": "",
99+
"join_local_fkey_name": "",
100+
"join_local_column": "",
101+
"join_local_column_nullable": false,
102+
"join_local_column_unique": false,
103+
"join_foreign_fkey_name": "",
104+
"join_foreign_column": "",
105+
"join_foreign_column_nullable": false,
106+
"join_foreign_column_unique": false
107+
},
108+
{
109+
"name": "FK_node_root",
110+
"table": "node",
111+
"column": "id",
112+
"nullable": false,
113+
"unique": true,
114+
"foreign_table": "node",
115+
"foreign_column": "root_id",
116+
"foreign_column_nullable": false,
117+
"foreign_column_unique": false,
118+
"to_join_table": false,
119+
"join_table": "",
120+
"join_local_fkey_name": "",
121+
"join_local_column": "",
122+
"join_local_column_nullable": false,
123+
"join_local_column_unique": false,
124+
"join_foreign_fkey_name": "",
125+
"join_foreign_column": "",
126+
"join_foreign_column_nullable": false,
127+
"join_foreign_column_unique": false
128+
}
129+
],
130+
"is_view": false,
131+
"view_capabilities": {
132+
"can_insert": false,
133+
"can_upsert": false
134+
}
135+
},
4136
{
5137
"name": "sponsors",
6138
"schema_name": "",

drivers/sqlboiler-mssql/driver/testdatabase.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SET QUOTED_IDENTIFIER ON;
22

33
-- Don't forget to maintain order here, foreign keys!
4+
drop table if exists node;
45
drop table if exists video_tags;
56
drop table if exists tags;
67
drop table if exists videos;
@@ -158,6 +159,18 @@ create table type_monsters (
158159
generated_virtual AS smallint_nnull * smallint_null
159160
);
160161

162+
create table node (
163+
id int primary key,
164+
parent_id int,
165+
root_id int not null,
166+
167+
constraint CK_parent_root check((parent_id is not null and root_id != id) OR (parent_id is null and root_id = id)),
168+
constraint UN_node_parent_root unique(id, root_id),
169+
constraint FK_node_parent foreign key (parent_id) references node(id),
170+
constraint FK_node_root foreign key(root_id) references node(id),
171+
constraint FK_node_parent_root foreign key (parent_id, root_id) references node(id, root_id)
172+
);
173+
161174
GO
162175

163176
create view user_videos as

drivers/sqlboiler-mysql/driver/mysql.golden.enums.json

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,138 @@
11
{
22
"schema": "",
33
"tables": [
4+
{
5+
"name": "node",
6+
"schema_name": "",
7+
"columns": [
8+
{
9+
"name": "id",
10+
"type": "int",
11+
"db_type": "int",
12+
"default": "",
13+
"comment": "",
14+
"nullable": false,
15+
"unique": true,
16+
"validated": false,
17+
"auto_generated": false,
18+
"arr_type": null,
19+
"udt_name": "",
20+
"domain_name": null,
21+
"full_db_type": "int"
22+
},
23+
{
24+
"name": "parent_id",
25+
"type": "null.Int",
26+
"db_type": "int",
27+
"default": "",
28+
"comment": "",
29+
"nullable": true,
30+
"unique": false,
31+
"validated": false,
32+
"auto_generated": false,
33+
"arr_type": null,
34+
"udt_name": "",
35+
"domain_name": null,
36+
"full_db_type": "int"
37+
},
38+
{
39+
"name": "root_id",
40+
"type": "int",
41+
"db_type": "int",
42+
"default": "",
43+
"comment": "",
44+
"nullable": false,
45+
"unique": false,
46+
"validated": false,
47+
"auto_generated": false,
48+
"arr_type": null,
49+
"udt_name": "",
50+
"domain_name": null,
51+
"full_db_type": "int"
52+
}
53+
],
54+
"p_key": {
55+
"name": "PRIMARY",
56+
"columns": [
57+
"id"
58+
]
59+
},
60+
"f_keys": [
61+
{
62+
"table": "node",
63+
"name": "FK_node_parent",
64+
"column": "parent_id",
65+
"nullable": true,
66+
"unique": false,
67+
"foreign_table": "node",
68+
"foreign_column": "id",
69+
"foreign_column_nullable": false,
70+
"foreign_column_unique": true
71+
},
72+
{
73+
"table": "node",
74+
"name": "FK_node_root",
75+
"column": "root_id",
76+
"nullable": false,
77+
"unique": false,
78+
"foreign_table": "node",
79+
"foreign_column": "id",
80+
"foreign_column_nullable": false,
81+
"foreign_column_unique": true
82+
}
83+
],
84+
"is_join_table": false,
85+
"to_one_relationships": null,
86+
"to_many_relationships": [
87+
{
88+
"name": "FK_node_parent",
89+
"table": "node",
90+
"column": "id",
91+
"nullable": false,
92+
"unique": true,
93+
"foreign_table": "node",
94+
"foreign_column": "parent_id",
95+
"foreign_column_nullable": true,
96+
"foreign_column_unique": false,
97+
"to_join_table": false,
98+
"join_table": "",
99+
"join_local_fkey_name": "",
100+
"join_local_column": "",
101+
"join_local_column_nullable": false,
102+
"join_local_column_unique": false,
103+
"join_foreign_fkey_name": "",
104+
"join_foreign_column": "",
105+
"join_foreign_column_nullable": false,
106+
"join_foreign_column_unique": false
107+
},
108+
{
109+
"name": "FK_node_root",
110+
"table": "node",
111+
"column": "id",
112+
"nullable": false,
113+
"unique": true,
114+
"foreign_table": "node",
115+
"foreign_column": "root_id",
116+
"foreign_column_nullable": false,
117+
"foreign_column_unique": false,
118+
"to_join_table": false,
119+
"join_table": "",
120+
"join_local_fkey_name": "",
121+
"join_local_column": "",
122+
"join_local_column_nullable": false,
123+
"join_local_column_unique": false,
124+
"join_foreign_fkey_name": "",
125+
"join_foreign_column": "",
126+
"join_foreign_column_nullable": false,
127+
"join_foreign_column_unique": false
128+
}
129+
],
130+
"is_view": false,
131+
"view_capabilities": {
132+
"can_insert": false,
133+
"can_upsert": false
134+
}
135+
},
4136
{
5137
"name": "sponsors",
6138
"schema_name": "",

0 commit comments

Comments
 (0)