keonik / prisma-erd-generator

Generate an ER Diagram based on your Prisma schema every time you run npx prisma generate
https://www.npmjs.com/package/prisma-erd-generator
MIT License
864 stars 47 forks source link

Unnecessary Many-to-Many Relation Lines in ER Diagram #219

Open mrsekut opened 1 year ago

mrsekut commented 1 year ago

Hello,

I've encountered an issue concerning the representation of relations between tables in the generated diagram.

In my schema, I have many models with various relations between them. The problem arises when two tables have a relation; it seems that the tool always draws an extra line representing a many-to-many relationship, in addition to the one that represents the actual relationship defined in the schema. This effectively doubles the number of lines in the diagram, leading to a lot of unnecessary clutter and making it difficult to read and understand.

As an example, consider the following schema involving Schedule and DailySchedule found in this link:

model DailySchedule {
  id         Int      @id @default(autoincrement())
  day        String
  startTime  String
  endTime    String
  schedule   Schedule @relation(fields: [scheduleId], references: [id])
  scheduleId Int
}

model Schedule {
  id             Int             @id @default(autoincrement())
  name           String
  timezone       String
  dailySchedules DailySchedule[]
  owner          User            @relation(fields: [ownerId], references: [id])
  ownerId        Int
  Meeting        Meeting[]
}

The ER diagram generated for this schema, which can be found here, shows two lines between Schedule and DailySchedule:

image

  1. A one-to-many line from Schedule to DailySchedule (accurate), labelled "schedule".
  2. An extra many-to-many line between Schedule and DailySchedule (not present in the schema), labelled "dailySchedules".

The second line, representing a many-to-many relationship that doesn't exist in the schema, is unnecessary and confusing.

I've looked through the existing issues and couldn't find a similar problem reported. Therefore, I am wondering if there's a way to prevent these unnecessary lines from being drawn or if this is a potential area for enhancement in the tool.

Thank you for your time and your work on this project.

keonik commented 1 year ago

What this is currently sending to mermaid is...

erDiagram

  "DailySchedule" {
    Int id "🗝️"
    String day
    String startTime
    String endTime
    }

  "Schedule" {
    Int id "🗝️"
    String name
    String timezone
    }

    "DailySchedule" o|--|| "Schedule" : "schedule"
    "Schedule" o{--}o "DailySchedule" : "dailySchedules"

The part in question

    "DailySchedule" o|--|| "Schedule" : "schedule"
    "Schedule" o{--}o "DailySchedule" : "dailySchedules"

Relationship syntax should show that there should be exactly one "Schedule" to zero to many "DailySchedules"

ignoring the generator it should be outputting as follows

   "DailySchedule" o{--|| "Schedule" : "schedule"
   "Schedule" ||--}o "DailySchedule" : "dailySchedules"

which would come back with...

erDiagram

  "DailySchedule" {
    Int id "🗝️"
    String day
    String startTime
    String endTime
    }

  "Schedule" {
    Int id "🗝️"
    String name
    String timezone
    }

   "DailySchedule" o{--|| "Schedule" : "schedule"
   "Schedule" ||--}o "DailySchedule" : "dailySchedules"

To remove/replace having both directions would have to be an additional feature. Somewhere in this issue list, there was a request to include both sides of the relationship because of the labeling having different meanings in each direction. But the relationships should be the same, which is why this is a bug. Unable to take a look now but happy to review a PR if you find it before I do

mrsekut commented 1 year ago

Thank you for your response.

I've tried to look into the code and find a way to fix this issue, but it seems quite challenging to resolve it quickly. I kindly ask you to consider revisiting this for a correction when you have some spare time.

keonik commented 1 year ago

Got it! Fix coming out to alpha shortly. I ended up searching for the relationship name on the other side and combining it with the initial. It then skips that relation so it doesn't send in the same relationship twice

Screenshot 2023-08-22 at 10 08 55 AM
DarinKumarnsit commented 12 months ago

@keonik would you mind show how to combining relationship names from both model in to one connection string?

keonik commented 12 months ago

Sorry about that. I've been out on vacation for a week. I'll pick this back up when I can. I left off getting the connection names correctly but some oddness with the relationship lines not showing up properly for the many to x instances.

github-actions[bot] commented 10 months ago

:tada: This issue has been resolved in version 1.12.0-alpha.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

hlehmann commented 7 months ago

The alpha version seems to work well, any plan to release in non alpha ?

keonik commented 7 months ago

It's been so long since I've checked but the last I saw on alpha was that the many-to-many relationships were still not working in all test cases. I'll give this a look tonight. Appreciate the ping! Sorry, I haven't been able to dedicate as much time to this.

hiroiku commented 1 month ago

When reversing the order of defining the Schedule model and the DailySchedule model, a one-to-many relationship incorrectly becomes a many-to-many relationship.

DailySchedule -> Schedule

DailySchedule -  Schedule

Schedule -> DailySchedule

Schedule -  DailySchedule