sql-to-jdl icon indicating copy to clipboard operation
sql-to-jdl copied to clipboard

Add support for enforcing creation of entity for many-to-many table

Open sheryarshirazi opened this issue 2 years ago • 5 comments

is there any option to enforce to generate entity of many-to-many table having 2 columns as foreign key

sheryarshirazi avatar Jan 07 '23 06:01 sheryarshirazi

I do not understand, can you show UML/example?

Blackdread avatar Jan 08 '23 09:01 Blackdread

see sql tables (users, locations and user_locations) below

CREATE TABLE users (
	id int(11) NOT NULL,
	full_name VARCHAR(100) NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE locations (
  id int(11) NOT NULL,
  street varchar(30) NOT NULL,
  city varchar(30) NOT NULL,
  state varchar(30) NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE users_locations (
  user_id INT,
  location_id INT,
  CONSTRAINT user_add_pk PRIMARY KEY (user_id, location_id),
  CONSTRAINT FK_user FOREIGN KEY (user_id) REFERENCES users (id),
  CONSTRAINT FK_location FOREIGN KEY (location_id) REFERENCES locations (id)
);

ERD

but generated jdl as

entity Locations(locations) {
    id Integer required,
    street String required maxlength(30),
    city String required maxlength(30),
    state String required maxlength(30)
}

entity Users(users) {
    id Integer required,
    fullName String required maxlength(100),
}

// Relations

// TODO This is a pure ManyToMany relation (delete me and decide owner side)
relationship ManyToMany {
    UsersLocations{user required} to Users{usersLocations}
}

// TODO This is a pure ManyToMany relation (delete me and decide owner side)
relationship ManyToMany {
    UsersLocations{location required} to Locations{usersLocations}
}

it omits bridge table (connecting table) if table has only foreign keys. You have also mentioned in readme so my question is in this regard that can we enforce to generate this bridge table entity. one thing if we add 3rd column in bridge table it will generate entity.

sheryarshirazi avatar Jan 08 '23 10:01 sheryarshirazi

As I understand, this a feature request for forcing an entity creation of UsersLocations and have 2 ManyToOne relations defined, am I correct? I see the feature configuration could be:

  • Add boolean to specify if force all ManyToMany as 2 ManyToOne (default is false) and generate entities
  • Add list of table names to override default for specified tables to force ManyToMany as 2 ManyToOne and make those tables as entities

As you have noted, adding an extra column in users_locations will not consider that table as a ManyToMany which is correct current behaviour.

If you want to add that feature, please follow on above requirements given.

Blackdread avatar Jan 08 '23 11:01 Blackdread

you are right.

sheryarshirazi avatar Jan 08 '23 12:01 sheryarshirazi

I have a bit of the opposite situation in a database I am working on. There is a table with two foreign key columns, but also has an updated and created timestamp that are set with a database trigger. In this case I want it to be considered a ManyToMany, but it is treated as a separate entity. I will look into this if I get around to handling my situation.

octgsoftware avatar Jan 12 '23 14:01 octgsoftware