Many-to-one relationships

In a many-to-one relationship one entity contains values that refer to another entity that has unique values. In the Hasura data APIs, many-to-one relationships are referred to as object relationships.

For example, in an article author schema, multiple articles can have the same unique author.

Creating a many-to-one relationship

To add a many-to-one relationship from article to author,

  1. Create a foreign key constraint on the article table to the author table.
  2. Open the API console, navigate to Data > article > Relationships section.
  3. Add the suggested object relationship and give it a desired name.
../../../../_images/many-to-one-rel.png

Fetching over a many-to-one relationship

To fetch the list of all articles along with the name of each of their author, use a query like so:

POST /v1/query HTTP/1.1
Content-Type: application/json
Authorization: Bearer <auth-token> # optional if cookie is set
X-Hasura-Role: <role>  # optional. Pass if only specific user role has access
query fetch_article {
   article {
       title
       author {
          name
       }
   }
}
POST data.<cluster-name>.hasura-app.io/v1/query HTTP/1.1
Content-Type: application/json
Authorization: Bearer <auth-token> # optional if cookie is set
X-Hasura-Role: admin

{
    "type" : "select",
    "args" : {
        "table" : "article",
        "columns": [
            "title",
            {
                "name": "author",
                "columns": ["name"]
            }
        ]
    }
}