0

I'm trying to generate a many to many relationship in Telosys, but it seems that it doesn't work. Automatic new model creation (nm command) generates the below code (with all "one to many" relationships – and the join table users_roles is recognized as a normal table):

@DbTable(roles)

@DbCatalog(lazycat)

Roles {

// attributes

id : int { @Id @AutoIncremented @DbName(id) @DbType(INT) @NotNull @NotEmpty };

name : string { @DbName(name) @DbType(“VARCHAR(50)”) @Size(50) @MaxLen(50) };

// links

usersrolesList : UsersRoles[];

}
@DbTable(users)

@DbCatalog(lazycat)

Users {

// attributes

id : int { @Id @AutoIncremented @DbName(id) @DbType(INT) @NotNull @NotEmpty };

user : string { @DbName(user) @DbType(“VARCHAR(50)”) @Size(50) @MaxLen(50) };

password : string { @DbName(password) @DbType(“VARCHAR(50)”) @Size(50) @MaxLen(50) };

// links

usersrolesList : UsersRoles[];

}
@DbTable(usersroles)

@DbCatalog(lazycat)

UsersRoles {

// attributes

idUser : int { @DbName(iduser) @DbType(INT) @FK(FK_users, Users.id) };

idRole : int { @DbName(idrole) @DbType(INT) @FK(FK_roles, Roles.id) };

// links

roles : Roles { @LinkByFK(FKroles) };

users : Users { @LinkByFK(FK_users) };

}

Below I report the SQL code to create the tables:

CREATE TABLE roles (

id INT(11) NOT NULL AUTO_INCREMENT,

name VARCHAR(50) NULL DEFAULT NULL COLLATE ‘utf8mb4generalci’,

PRIMARY KEY (id) USING BTREE

)

COLLATE='utf8mb4generalci'

ENGINE=InnoDB

AUTO_INCREMENT=2

;

CREATE TABLE users (

id INT(11) NOT NULL AUTO_INCREMENT,

user VARCHAR(50) NULL DEFAULT NULL COLLATE ‘utf8mb4generalci’,

password VARCHAR(50) NULL DEFAULT NULL COLLATE ‘utf8mb4generalci’,

PRIMARY KEY (id) USING BTREE

)

COLLATE='utf8mb4generalci'

ENGINE=InnoDB

AUTO_INCREMENT=2

;

CREATE TABLE users_roles (

id_user INT(11) NULL DEFAULT NULL,

id_role INT(11) NULL DEFAULT NULL,

INDEX FK__users (id_user) USING BTREE,

INDEX FK__roles (id_role) USING BTREE,

CONSTRAINT FK__roles FOREIGN KEY (id_role) REFERENCES lazycat.roles (id) ON UPDATE NO ACTION ON DELETE NO ACTION,

CONSTRAINT FK__users FOREIGN KEY (id_user) REFERENCES lazycat.users (id) ON UPDATE NO ACTION ON DELETE NO ACTION

)

COLLATE='utf8mb4generalci'

ENGINE=InnoDB

;

Please could I know where I'm wrong?

Magnus
  • 41
  • 1
  • 1
  • 2

1 Answers1

0

You're not wrong, it's the behavior of the tool.

The links follow all the FK-PK relationships found in the database.

A model created from a database is only a “raw model”, so you will need to refine it to align with your own expectations.

The tool cannot presume whether a table should be considered as a “join table”, even if it only has 2 FK.

Perhaps it would be interesting to add an option to consider all these types of tables as de facto “join table” ?

Anyway, the join table (or “join entity”) must exist in the model to be referenced with the @LinkByJoinEntity(xx) annotation in a @ManyToMany link.

lgu
  • 2,342
  • 21
  • 29