RicardoPHP
RicardoPHP

Reputation: 574

Prevent duplicated relationships on a multi-file Plantuml ER Diagram

I am using PlantUML to built a ER diagram and my intention is to separate the database structure in multiple DER file views so I would have a "full-view" and a "system table's view" and maybe an "invoice group table's view". This way whenever I want to built a new view I can just create a new file and add the tables to it.

To prevent every time writing the entity attributes I thought to separate the entities into files, so table users would end up in a file named like entity.users.swd and the table user_role would be named like entity.user_role.swd. And whenever I want to present the entity I would just include the file as it should be. But I want to add every table relation into the table file, this way whenever presenting users every relationship to users will be printed as well. If I place the relations in the entity files and add them both at the view file it will duplicate the diagram.

For example:

entity.users.swd

@startuml
entity users {
  int id
  string name
  int business_unit_id
}
@enduml

entity.user_role.swd

@startuml
entity user_role {
  int user_id
  int role_id
}
@enduml

entity.roles.swd

@startuml
entity roles {
  int id
  string name
}
@enduml

entity.business_units.swd

@startuml
entity business_units {
  int id
  string name
}
@enduml

If I add them into a DER view file and the relationships into this DER view file the relationships are presented as it should be

der.full.swd

@startuml
!include ./entity.users.swd
!include ./entity.user_role.swd
!include ./entity.roles.swd
!include ./entity.emails.swd
users ||--o{ user_role
roles ||--o{ user_role
business_units ||--o{ users
@enduml

This would end up

enter image description here

Now let's evolve the sample to the problem at hand ...

I want to place all relation marks like users ||--o{ user_role in the entity files so whenever I place those in a diagram it will automatically present that the table has an external dependency without detailing the fields of the external table. users and user_role files would both have the relation users ||--o{ user_role like below, but if I do so it will duplicate the relationship in the final image below.

entity.users.swd

@startuml
entity users {
  int id
  string name
  int business_unit_id
}
users ||--o{ user_role
@enduml

entity.user_role.swd

@startuml
entity users {
  int id
  string name
  int business_unit_id
}
users ||--o{ user_role
roles ||--o{ user_role
@enduml

der.user-view.swd

@startuml
!include ./entity.users.swd
!include ./entity.user_role.swd
@enduml

How it builds the image:

enter image description here

How I want it:

enter image description here

How do I prevent duplicated relationships on this multi-file PlantUML ER Diagram?

Upvotes: 1

Views: 413

Answers (1)

Fuhrmanator
Fuhrmanator

Reputation: 12922

Setting a variable with the preprocessor can avoid declaring the relationship again when using includes. It's effectively an include guard (although I'm not sure PlantUML has a better way -- perhaps it should).

This isn't the whole solution, but just replace your definitions of users ||--o{ user_role (or whatever you don't want to repeat) with the following:

!if ($users!="defined")
users ||--o{ user_role
!$users = "defined"
!endif

Upvotes: 0

Related Questions