Skip to content

Commit

Permalink
Merge pull request #1299 from paulo-raca/ignore-composite-fk
Browse files Browse the repository at this point in the history
Ignore Composite Foreign Keys.
  • Loading branch information
stephenafamo authored Sep 4, 2023
2 parents 8b8a1c5 + 2cb4f9b commit 96378fa
Show file tree
Hide file tree
Showing 11 changed files with 867 additions and 9 deletions.
14 changes: 14 additions & 0 deletions drivers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,22 @@ func ColumnsFromList(list []string, tablename string) []string {
func CombineConfigAndDBForeignKeys(configForeignKeys []ForeignKey, tableName string, dbForeignKeys []ForeignKey) []ForeignKey {
combinedForeignKeys := make([]ForeignKey, 0, len(configForeignKeys)+len(dbForeignKeys))
appearedColumns := make(map[string]bool)
fkNameCount := make(map[string]int)

// Detect Composite Foreign Keys in the database by counting how many times they appear.
// boiler doesn't support Composite FKs and should ignore those
for _, fk := range dbForeignKeys {
fkNameCount[fk.Name] += 1
}

for _, fk := range configForeignKeys {
// need check table name here cause configForeignKeys contains all foreign keys of all tables
if fk.Table != tableName {
continue
}
if appearedColumns[fk.Column] {
continue
}

combinedForeignKeys = append(combinedForeignKeys, fk)
appearedColumns[fk.Column] = true
Expand All @@ -268,7 +278,11 @@ func CombineConfigAndDBForeignKeys(configForeignKeys []ForeignKey, tableName str
if appearedColumns[fk.Column] {
continue
}
if fkNameCount[fk.Name] != 1 {
continue
}
combinedForeignKeys = append(combinedForeignKeys, fk)
appearedColumns[fk.Column] = true
}

return combinedForeignKeys
Expand Down
132 changes: 132 additions & 0 deletions drivers/sqlboiler-mssql/driver/mssql.golden.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,138 @@
{
"schema": "dbo",
"tables": [
{
"name": "node",
"schema_name": "",
"columns": [
{
"name": "id",
"type": "int",
"db_type": "int",
"default": "",
"comment": "",
"nullable": false,
"unique": true,
"validated": false,
"auto_generated": false,
"arr_type": null,
"udt_name": "",
"domain_name": null,
"full_db_type": "int"
},
{
"name": "parent_id",
"type": "null.Int",
"db_type": "int",
"default": "",
"comment": "",
"nullable": true,
"unique": false,
"validated": false,
"auto_generated": false,
"arr_type": null,
"udt_name": "",
"domain_name": null,
"full_db_type": "int"
},
{
"name": "root_id",
"type": "int",
"db_type": "int",
"default": "",
"comment": "",
"nullable": false,
"unique": false,
"validated": false,
"auto_generated": false,
"arr_type": null,
"udt_name": "",
"domain_name": null,
"full_db_type": "int"
}
],
"p_key": {
"name": "PK__node",
"columns": [
"id"
]
},
"f_keys": [
{
"table": "node",
"name": "FK_node_parent",
"column": "parent_id",
"nullable": true,
"unique": false,
"foreign_table": "node",
"foreign_column": "id",
"foreign_column_nullable": false,
"foreign_column_unique": true
},
{
"table": "node",
"name": "FK_node_root",
"column": "root_id",
"nullable": false,
"unique": false,
"foreign_table": "node",
"foreign_column": "id",
"foreign_column_nullable": false,
"foreign_column_unique": true
}
],
"is_join_table": false,
"to_one_relationships": null,
"to_many_relationships": [
{
"name": "FK_node_parent",
"table": "node",
"column": "id",
"nullable": false,
"unique": true,
"foreign_table": "node",
"foreign_column": "parent_id",
"foreign_column_nullable": true,
"foreign_column_unique": false,
"to_join_table": false,
"join_table": "",
"join_local_fkey_name": "",
"join_local_column": "",
"join_local_column_nullable": false,
"join_local_column_unique": false,
"join_foreign_fkey_name": "",
"join_foreign_column": "",
"join_foreign_column_nullable": false,
"join_foreign_column_unique": false
},
{
"name": "FK_node_root",
"table": "node",
"column": "id",
"nullable": false,
"unique": true,
"foreign_table": "node",
"foreign_column": "root_id",
"foreign_column_nullable": false,
"foreign_column_unique": false,
"to_join_table": false,
"join_table": "",
"join_local_fkey_name": "",
"join_local_column": "",
"join_local_column_nullable": false,
"join_local_column_unique": false,
"join_foreign_fkey_name": "",
"join_foreign_column": "",
"join_foreign_column_nullable": false,
"join_foreign_column_unique": false
}
],
"is_view": false,
"view_capabilities": {
"can_insert": false,
"can_upsert": false
}
},
{
"name": "sponsors",
"schema_name": "",
Expand Down
13 changes: 13 additions & 0 deletions drivers/sqlboiler-mssql/driver/testdatabase.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SET QUOTED_IDENTIFIER ON;

-- Don't forget to maintain order here, foreign keys!
drop table if exists node;
drop table if exists video_tags;
drop table if exists tags;
drop table if exists videos;
Expand Down Expand Up @@ -158,6 +159,18 @@ create table type_monsters (
generated_virtual AS smallint_nnull * smallint_null
);

create table node (
id int primary key,
parent_id int,
root_id int not null,

constraint CK_parent_root check((parent_id is not null and root_id != id) OR (parent_id is null and root_id = id)),
constraint UN_node_parent_root unique(id, root_id),
constraint FK_node_parent foreign key (parent_id) references node(id),
constraint FK_node_root foreign key(root_id) references node(id),
constraint FK_node_parent_root foreign key (parent_id, root_id) references node(id, root_id)
);

GO

create view user_videos as
Expand Down
132 changes: 132 additions & 0 deletions drivers/sqlboiler-mysql/driver/mysql.golden.enums.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,138 @@
{
"schema": "",
"tables": [
{
"name": "node",
"schema_name": "",
"columns": [
{
"name": "id",
"type": "int",
"db_type": "int",
"default": "",
"comment": "",
"nullable": false,
"unique": true,
"validated": false,
"auto_generated": false,
"arr_type": null,
"udt_name": "",
"domain_name": null,
"full_db_type": "int"
},
{
"name": "parent_id",
"type": "null.Int",
"db_type": "int",
"default": "",
"comment": "",
"nullable": true,
"unique": false,
"validated": false,
"auto_generated": false,
"arr_type": null,
"udt_name": "",
"domain_name": null,
"full_db_type": "int"
},
{
"name": "root_id",
"type": "int",
"db_type": "int",
"default": "",
"comment": "",
"nullable": false,
"unique": false,
"validated": false,
"auto_generated": false,
"arr_type": null,
"udt_name": "",
"domain_name": null,
"full_db_type": "int"
}
],
"p_key": {
"name": "PRIMARY",
"columns": [
"id"
]
},
"f_keys": [
{
"table": "node",
"name": "FK_node_parent",
"column": "parent_id",
"nullable": true,
"unique": false,
"foreign_table": "node",
"foreign_column": "id",
"foreign_column_nullable": false,
"foreign_column_unique": true
},
{
"table": "node",
"name": "FK_node_root",
"column": "root_id",
"nullable": false,
"unique": false,
"foreign_table": "node",
"foreign_column": "id",
"foreign_column_nullable": false,
"foreign_column_unique": true
}
],
"is_join_table": false,
"to_one_relationships": null,
"to_many_relationships": [
{
"name": "FK_node_parent",
"table": "node",
"column": "id",
"nullable": false,
"unique": true,
"foreign_table": "node",
"foreign_column": "parent_id",
"foreign_column_nullable": true,
"foreign_column_unique": false,
"to_join_table": false,
"join_table": "",
"join_local_fkey_name": "",
"join_local_column": "",
"join_local_column_nullable": false,
"join_local_column_unique": false,
"join_foreign_fkey_name": "",
"join_foreign_column": "",
"join_foreign_column_nullable": false,
"join_foreign_column_unique": false
},
{
"name": "FK_node_root",
"table": "node",
"column": "id",
"nullable": false,
"unique": true,
"foreign_table": "node",
"foreign_column": "root_id",
"foreign_column_nullable": false,
"foreign_column_unique": false,
"to_join_table": false,
"join_table": "",
"join_local_fkey_name": "",
"join_local_column": "",
"join_local_column_nullable": false,
"join_local_column_unique": false,
"join_foreign_fkey_name": "",
"join_foreign_column": "",
"join_foreign_column_nullable": false,
"join_foreign_column_unique": false
}
],
"is_view": false,
"view_capabilities": {
"can_insert": false,
"can_upsert": false
}
},
{
"name": "sponsors",
"schema_name": "",
Expand Down
Loading

0 comments on commit 96378fa

Please sign in to comment.