From c35d26de186b1093108d9286db3c9afb02cc5653 Mon Sep 17 00:00:00 2001 From: Huy Le Date: Fri, 1 Nov 2024 00:18:26 +0700 Subject: [PATCH] Fix cannot get foreign keys with same names --- .../postgres/expect-out-files/schema.dbml | 18 +++ .../__test__/db2dbml/postgres/schema.sql | 49 ++++++ .../postgres/expect-out-files/schema.json | 153 ++++++++++++++++++ .../src/connectors/postgresConnector.ts | 1 + 4 files changed, 221 insertions(+) diff --git a/packages/dbml-cli/__test__/db2dbml/postgres/expect-out-files/schema.dbml b/packages/dbml-cli/__test__/db2dbml/postgres/expect-out-files/schema.dbml index 291fa43b7..3ae18a3fa 100644 --- a/packages/dbml-cli/__test__/db2dbml/postgres/expect-out-files/schema.dbml +++ b/packages/dbml-cli/__test__/db2dbml/postgres/expect-out-files/schema.dbml @@ -171,6 +171,20 @@ Table "table2" { "status" enum_type2 } +Table "table_same_fk" { + "id" int4 [pk, not null, increment] +} + +Table "table_same_fk_1" { + "id" int4 [pk, not null, increment] + "fk_id_1" int4 +} + +Table "table_same_fk_2" { + "id" int4 [pk, not null, increment] + "fk_id_2" int4 +} + Ref "fk_authornationality":"authors".("authorid", "nationalityid") < "books".("authorid", "nationalityid") [delete: cascade] Ref "fk_order":"orders"."order_id" < "order_items"."order_id" [delete: cascade] @@ -178,3 +192,7 @@ Ref "fk_order":"orders"."order_id" < "order_items"."order_id" [delete: cascade] Ref "fk_product":"products"."product_id" < "order_items"."product_id" [delete: cascade] Ref "fk_user":"users"."user_id" < "orders"."user_id" [delete: cascade] + +Ref "fk_same_name_test":"table_same_fk"."id" < "table_same_fk_1"."fk_id_1" + +Ref "fk_same_name_test":"table_same_fk"."id" < "table_same_fk_2"."fk_id_2" diff --git a/packages/dbml-cli/__test__/db2dbml/postgres/schema.sql b/packages/dbml-cli/__test__/db2dbml/postgres/schema.sql index 743fd2e69..5b992ec6f 100644 --- a/packages/dbml-cli/__test__/db2dbml/postgres/schema.sql +++ b/packages/dbml-cli/__test__/db2dbml/postgres/schema.sql @@ -185,3 +185,52 @@ CREATE TABLE dbml_test.table3 ( id SERIAL PRIMARY KEY, status dbml_test.enum_type3 ); + +-- Foreign keys with same names +CREATE TABLE table_same_fk ( + id SERIAL PRIMARY KEY +); + +CREATE TABLE table_same_fk_1 ( + id SERIAL PRIMARY KEY, + fk_id_1 INT +); + +CREATE TABLE table_same_fk_2 ( + id SERIAL PRIMARY KEY, + fk_id_2 INT +); + +ALTER TABLE table_same_fk_1 +ADD CONSTRAINT fk_same_name_test +FOREIGN KEY (fk_id_1) REFERENCES table_same_fk(id); + +ALTER TABLE table_same_fk_2 +ADD CONSTRAINT fk_same_name_test +FOREIGN KEY (fk_id_2) REFERENCES table_same_fk(id); + +-- Composite foreign keys with same names +-- CREATE TABLE table_same_composite_fk ( +-- ref_id_1 SERIAL, +-- ref_id_2 SERIAL, + +-- CONSTRAINT pk_same_fk PRIMARY KEY (ref_id_1, ref_id_2) +-- ); + +-- CREATE TABLE table_same_composite_fk_1 ( +-- fk_id1_1 INT, +-- fk_id1_2 INT +-- ); + +-- CREATE TABLE table_same_composite_fk_2 ( +-- fk_id2_1 INT, +-- fk_id2_2 INT +-- ); + +-- ALTER TABLE table_same_composite_fk_1 +-- ADD CONSTRAINT fk_same_name_test +-- FOREIGN KEY (fk_id1_1, fk_id1_2) REFERENCES table_same_composite_fk(ref_id_1, ref_id_2); + +-- ALTER TABLE table_same_composite_fk_2 +-- ADD CONSTRAINT fk_same_name_test +-- FOREIGN KEY (fk_id2_1, fk_id2_2) REFERENCES table_same_composite_fk(ref_id_1, ref_id_2); diff --git a/packages/dbml-connector/__tests__/connectors/postgres/expect-out-files/schema.json b/packages/dbml-connector/__tests__/connectors/postgres/expect-out-files/schema.json index 98f6708b6..7689d5fd1 100644 --- a/packages/dbml-connector/__tests__/connectors/postgres/expect-out-files/schema.json +++ b/packages/dbml-connector/__tests__/connectors/postgres/expect-out-files/schema.json @@ -97,6 +97,27 @@ "note": { "value": "" } + }, + { + "name": "table_same_fk", + "schemaName": "public", + "note": { + "value": "" + } + }, + { + "name": "table_same_fk_1", + "schemaName": "public", + "note": { + "value": "" + } + }, + { + "name": "table_same_fk_2", + "schemaName": "public", + "note": { + "value": "" + } } ], "fields": { @@ -1270,6 +1291,77 @@ "value": "" } } + ], + "public.table_same_fk": [ + { + "name": "id", + "type": { + "type_name": "int4", + "schemaName": null + }, + "dbdefault": null, + "not_null": true, + "increment": true, + "note": { + "value": "" + } + } + ], + "public.table_same_fk_1": [ + { + "name": "id", + "type": { + "type_name": "int4", + "schemaName": null + }, + "dbdefault": null, + "not_null": true, + "increment": true, + "note": { + "value": "" + } + }, + { + "name": "fk_id_1", + "type": { + "type_name": "int4", + "schemaName": null + }, + "dbdefault": null, + "not_null": false, + "increment": false, + "note": { + "value": "" + } + } + ], + "public.table_same_fk_2": [ + { + "name": "id", + "type": { + "type_name": "int4", + "schemaName": null + }, + "dbdefault": null, + "not_null": true, + "increment": true, + "note": { + "value": "" + } + }, + { + "name": "fk_id_2", + "type": { + "type_name": "int4", + "schemaName": null + }, + "dbdefault": null, + "not_null": false, + "increment": false, + "note": { + "value": "" + } + } ] }, "refs": [ @@ -1366,6 +1458,52 @@ ], "onDelete": "CASCADE", "onUpdate": null + }, + { + "name": "fk_same_name_test", + "endpoints": [ + { + "tableName": "table_same_fk_1", + "schemaName": "public", + "fieldNames": [ + "fk_id_1" + ], + "relation": "*" + }, + { + "tableName": "table_same_fk", + "schemaName": "public", + "fieldNames": [ + "id" + ], + "relation": "1" + } + ], + "onDelete": null, + "onUpdate": null + }, + { + "name": "fk_same_name_test", + "endpoints": [ + { + "tableName": "table_same_fk_2", + "schemaName": "public", + "fieldNames": [ + "fk_id_2" + ], + "relation": "*" + }, + { + "tableName": "table_same_fk", + "schemaName": "public", + "fieldNames": [ + "id" + ], + "relation": "1" + } + ], + "onDelete": null, + "onUpdate": null } ], "enums": [ @@ -1594,6 +1732,21 @@ "pk": true } }, + "public.table_same_fk": { + "id": { + "pk": true + } + }, + "public.table_same_fk_1": { + "id": { + "pk": true + } + }, + "public.table_same_fk_2": { + "id": { + "pk": true + } + }, "public.table_with_comments": { "id": { "pk": true diff --git a/packages/dbml-connector/src/connectors/postgresConnector.ts b/packages/dbml-connector/src/connectors/postgresConnector.ts index 2918fcf36..d44168915 100644 --- a/packages/dbml-connector/src/connectors/postgresConnector.ts +++ b/packages/dbml-connector/src/connectors/postgresConnector.ts @@ -240,6 +240,7 @@ const generateRawRefs = async (client: Client, schemas: string[]): Promise