Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mrsekut opened this issue Jul 12, 2023 · 9 comments · Fixed by #237
Open

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

mrsekut opened this issue Jul 12, 2023 · 9 comments · Fixed by #237
Labels
bug Something isn't working released on @alpha

Comments

@mrsekut
Copy link
Contributor

mrsekut commented Jul 12, 2023

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 keonik added the bug Something isn't working label Jul 14, 2023
@keonik
Copy link
Owner

keonik commented Jul 14, 2023

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"
Loading

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"
Loading

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
Copy link
Contributor Author

mrsekut commented Jul 15, 2023

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
Copy link
Owner

keonik commented Aug 22, 2023

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

@keonik keonik mentioned this issue Aug 22, 2023
@DarinKumarnsit
Copy link

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

@keonik
Copy link
Owner

keonik commented Sep 1, 2023

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
Copy link

github-actions bot commented Oct 9, 2023

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

The release is available on:

Your semantic-release bot 📦🚀

@hlehmann
Copy link

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

@keonik
Copy link
Owner

keonik commented Jan 29, 2024

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
Copy link

hiroiku commented Jul 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released on @alpha
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants