Blackdread / sql-to-jdl

Tool to translate SQL databases to JDL format of jHipster (Created due to existing databases to be generated with jHipster and build angular-java web)
MIT License
179 stars 81 forks source link

Fix a bug 'relationship name is not synchronized' #169

Closed gleb619 closed 1 year ago

gleb619 commented 1 year ago

When there are 2 relations, many to one, there is an error: The name of the relation is not synchronized.

Postgres example:

CREATE TABLE worker (
    id bigserial NOT NULL,
    "name" varchar NOT NULL,
    details varchar NULL,
    CONSTRAINT worker_pkey PRIMARY KEY (id)
);

CREATE TABLE task (
    id bigserial NOT NULL,
    "name" varchar NOT NULL,
    summary varchar NULL,
    description varchar NULL,
    priority varchar NULL,
    assignee_id int8 NULL,
    author_id int8 NULL,
    status varchar NULL,
    CONSTRAINT task_pkey PRIMARY KEY (id),
    CONSTRAINT task_assignee_id_fkey FOREIGN KEY (assignee_id) REFERENCES worker(id),
    CONSTRAINT task_author_id_fkey FOREIGN KEY (author_id) REFERENCES worker(id)
);

For this ddl, next jdl will be generated:

entity Task(task) {
    name String required maxlength(255),
    summary String maxlength(255),
    description String maxlength(255),
    priority String maxlength(255),
    status String maxlength(255)
}

entity Worker(worker) {
    name String required maxlength(255),
    details String maxlength(255)
}

// Relations
relationship ManyToOne {
    Task{assignee} to Worker{task}
}

relationship ManyToOne {
    Task{author} to Worker{task}
}

As you see, we have two ManyToOne relationship with the same name 'task'

This will lead us to the next jhipster error:

An error occured while running jhipster:entity#prepareRelationshipsForTemplates
Error running generator entities: Error: Error at entity Task: relationship name is not synchronized {                                                                                    
    "otherEntityName": "worker",
    "otherEntityRelationshipName": "task",
    "relationshipName": "author",
    "relationshipType": "many-to-one",
    "otherEntity": "[Worker Entity]",
    "otherEntityField": "id",
    "ownerSide": true,
    "collection": false,
    "otherSideReferenceExists": false,
    "otherEntityIsEmbedded": false
} with {
    "otherEntityName": "task",
    "otherEntityRelationshipName": "assignee",
    "relationshipName": "task",
    "relationshipType": "one-to-many",
    "otherEntity": "[Task Entity]"
}

It is necessary to correct this error. To do this, we need to add to the method JdlService.buildRelation an additional check to create the name

gleb619 commented 1 year ago

Added a fix, @Blackdread please check

Blackdread commented 1 year ago

I can see the error thrown in generator sources but not sure why this is an issue. This is only the name of a field, not duplicated in same entity, right?

Blackdread commented 1 year ago

Thank you, field name is indeed duplicated, PR merged

Blackdread commented 1 year ago

@gleb619 I pushed some changes after merge your PR

gleb619 commented 1 year ago

Thank you!