Skip to content
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

Prevent naming collisions #4

Open
nestorrente opened this issue Dec 28, 2023 · 0 comments
Open

Prevent naming collisions #4

nestorrente opened this issue Dec 28, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@nestorrente
Copy link
Owner

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:

  • Throw an error when this happen.
  • Add an incremental suffix to the generated tables and columns
    • For example, if the Role entity has an explicit user_id column, the column produced by the Role -> User relationship must be named user_id2 or something similar.
    • The same applies to the corresponding table of the Users entity. As users table already exists (because the User entity is declared before Users), then the table must be named users2.
    • Also, we could return a result with warnings in those cases, as those scenarios are likely to be errors.
@nestorrente nestorrente added the bug Something isn't working label Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant