2

I have two tables that represent a graph structure.

Location {
  id: Int!
  name: String!
}

Road {
  id: Int!
  name: String!
  loc1Id: Int!
  loc2Id: Int!
}

What I would like to be able to do is have relationship that is an array of Road on Location that is made up of both Road that reference a Location via either the loc1Id or loc2Id fields/columns.

If I try to select both in the console in a single relationship create I get only the second selected one in the created relationship (and no error).

Rory Hart
  • 1,813
  • 1
  • 19
  • 19

1 Answers1

0

You are trying to create a many to many relationship. The best way to create a many to many relationship is to have a join table.

location {
  id: Int!
  name: String!
}
Road {
  id: Int!
  name: String!
}
location_road {
  id: Int!
  road_id: Int!
  location_id: Int!
}

And create an array relationship between road and location_road and another array relationship between location and location_road

How to implement a many-to-many relationship in PostgreSQL?

Leonardo Alves
  • 1,876
  • 1
  • 16
  • 19
  • 1
    I'm trying to create a `2:*` relationship. If I use a join table then a Road could have more than 2 locations which isn't valid in my model. – Rory Hart Aug 08 '20 at 01:38
  • @RoryHart - Although the console relationships tab shows only one selected relationship , the following query should work if you created the relationship manually by selecting both. ```query { location { name roads { location1 { id name } location2 { id name } } } } ``` Is this what you are expecting? – praveenweb Aug 08 '20 at 07:27