drawdb-io / drawdb

Free, simple, and intuitive online database diagram editor and SQL generator.
https://drawdb.app
GNU Affero General Public License v3.0
22.7k stars 1.58k forks source link

[BUG] SQL Export for SQLite does not work with multiple FOREIGN KEYs. #226

Closed sunrize closed 3 months ago

sunrize commented 3 months ago

Describe the bug The SQL export results for the SQLite database only show one foreign key, while other foreign keys are missing.

To Reproduce Steps to reproduce the behavior:

  1. Create tables with two or more foreign key relationships.
  2. Export the SQL code for the SQLite database.
  3. Check the resulting SQL code.
    
    CREATE TABLE IF NOT EXISTS "table_0" (
    "table_1" INTEGER,
    "table_2" INTEGER,
    "id" INTEGER,
    PRIMARY KEY("id"),
    FOREIGN KEY ("table_1") REFERENCES "table_1"("id")
    ON UPDATE NO ACTION ON DELETE NO ACTION
        -- FOREIGN KEY ("table_2") ... is not present
    );

CREATE TABLE IF NOT EXISTS "table_1" ( "id" INTEGER NOT NULL UNIQUE, PRIMARY KEY("id")
);

CREATE TABLE IF NOT EXISTS "table_2" ( "id" INTEGER NOT NULL UNIQUE, PRIMARY KEY("id")
);


**Expected behavior**
All foreign keys should be present in the SQL export for the SQLite database.
```sql
CREATE TABLE IF NOT EXISTS "table_0" (
    "table_1" INTEGER,
    "table_2" INTEGER,
    "id" INTEGER,
    PRIMARY KEY("id"),
    FOREIGN KEY ("table_1") REFERENCES "table_1"("id")
    ON UPDATE NO ACTION ON DELETE NO ACTION
        FOREIGN KEY ("table_2") REFERENCES "table_2"("id")
    ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE IF NOT EXISTS "table_1" (
    "id" INTEGER NOT NULL UNIQUE,
    PRIMARY KEY("id")   
);

CREATE TABLE IF NOT EXISTS "table_2" (
    "id" INTEGER NOT NULL UNIQUE,
    PRIMARY KEY("id")   
); 

Screenshots Example Untitled Diagram_2024-08-24T05_51_38 908Z

Desktop (please complete the following information):

Additional context None

i-m-soumya commented 3 months ago

Hello @sunrize ,

I was able to replicate the issue under specific conditions. Here’s what I observed:

Issue Replication:

Scenario: When drawing relationships from table_0 to table_1 and table_2, the foreign keys are missing in the SQL export. Workaround: Drawing relationships from table_1 to table_0 and table_2 to table_0 does not result in missing foreign keys.

Hi @1ilit , I am looking into the issue. You can assign me this one.

1ilit commented 3 months ago

@i-m-soumya sorry just saw your comment. I already fixed it.

Workaround: Drawing relationships from table_1 to table_0 and table_2 to table_0 does not result in missing foreign keys.

This wouldn't work because then the foreign keys would be in table_1 and table_2 instead of table_0

i-m-soumya commented 3 months ago

Cool 😎

sunrize commented 2 months ago

Hello @1ilit, I noticed the same issue with the 'Generic' database export to SQLite. Thank you.

1ilit commented 2 months ago

@sunrize fixed, thanks for letting me know