One-to-one relationships

One-to-One (1-1) relationship is defined as the relationship between two tables where both the tables should be associated with each other based on only one matching row.

Lets consider two sample tables: person and voter

Table Columns
person id (p_key), name, age, gender
voter voter_id (p_key), person_id

In the above schema, a person can have only one voter_id and every voter_id can identify only one person.

Lets add a one-to-one relationship between the person and voter tables.

Creating a one-to-one relationship

To create a one-to-one relationship, we have to create a relationship over a foreign key created on unique columns in both the tables. In this case, we will add a uniqueness constraint over the person_id column in the voter table. In the case of person table, id is a primary key and therefore already unique.

  1. Open the API console and navigate to the Data > SQL section.

  2. Add uniqueness constraint:

    1. Add the following SQL command to create a uniqueness constraint on the person_id column of the voter table.

      CREATE UNIQUE INDEX ON voter (person_id)
      
    2. Check the This is a migration checkbox so that the query is saved as a migration.

    3. Hit Run.

  3. Create an object relationship from person to voter.

  4. Similarly, create an object relationship from voter to person.

Fetching over a one-to-one relationship

To fetch the list of all entries from the person table along with their voter_id:

query fetch_person {
    person {
        name
        age
        gender
        voter_info {
            voter_id
        }
    }
}
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" : "person",
        "columns": [
            "*",
            {
                "name": "voter_info",
                "columns": ["voter_id"]
            }
        ]
    }
}

Similarly, to fetch all the entries from the voter table along with the associated person info:

query fetch_voter {
   voter {
       id
       person_info {
          name
          age
          gender
       }
   }
}
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" : "voter",
        "columns": [
            "*",
            {
                "name": "person_info",
                "columns": ["*"]
            }
        ]
    }
}