nestorrente / erdiagram

Entity-Relationship diagram code generator library
MIT License
49 stars 14 forks source link

Prevent naming collisions #4

Open nestorrente opened 10 months ago

nestorrente commented 10 months ago

There are several scenarios where generated code (SQL, TS or Java) can produce several tables, columns or classes with the same name.

For example, if you define the following ER model:

User
    id int

Users

Role
    userId int

UsersRole

Role -> User
User *<->* Role

The following SQL code is produced for SQLite:

PRAGMA foreign_keys = OFF;

CREATE TABLE "users" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "id" INTEGER NOT NULL
);

CREATE TABLE "users" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);

CREATE TABLE "roles" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "user_id" INTEGER NOT NULL,
    "user_id" INTEGER NOT NULL,
    CONSTRAINT "roles_user_id_unique" UNIQUE ("user_id"),
    CONSTRAINT "roles_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "users" ("id")
);

CREATE TABLE "users_roles" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);

CREATE TABLE "users_roles" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "user_id" INTEGER NOT NULL,
    "role_id" INTEGER NOT NULL,
    CONSTRAINT "users_roles_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "users" ("id"),
    CONSTRAINT "users_roles_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "roles" ("id")
);

PRAGMA foreign_keys = ON;

The users and users_roles tables are duplicated, and the same happends to the roles.user_id column. We must implement one of the following solutions: