Skip to content

Fix cannot get foreign keys with same names #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,28 @@ 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]

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"
49 changes: 49 additions & 0 deletions packages/dbml-cli/__test__/db2dbml/postgres/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ const generateRawRefs = async (client: Client, schemas: string[]): Promise<Ref[]
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
AND tc.table_name = kcu.table_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
JOIN information_schema.referential_constraints AS rc
Expand Down
Loading