Creating a Hasura Relationship for Multiple Foreign Key Columns

Viewed 211

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).

1 Answers

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?

Related