Skip to content

Prevent naming collisions #4

Description

@nestorrente

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions