2020-01-14 15:57:45 +03:00
.. meta ::
:description: Manage relationships between tables/views in Hasura
:keywords: hasura, docs, schema, relationship
2020-03-03 15:02:40 +03:00
.. _relationships:
2018-09-11 14:11:24 +03:00
Relationships between tables/views
==================================
2018-12-03 15:12:24 +03:00
.. contents :: Table of contents
:backlinks: none
:depth: 1
:local:
2020-03-11 22:42:36 +03:00
To make :ref: `nested object queries <nested_object_queries>` , the tables/views in your database need to
2018-11-06 11:38:40 +03:00
be connected via relationships.
2018-09-11 14:11:24 +03:00
2018-11-06 11:38:40 +03:00
Let's say we have the following tables in our database: `` author `` , `` passport_info `` , `` article `` and `` tag `` .
2018-09-11 14:11:24 +03:00
2018-12-03 15:12:24 +03:00
Table relationships
-------------------
2018-11-06 11:38:40 +03:00
The tables/views in any relational database are typically related to each other via one of the
following types of table relationships:
2018-10-18 17:27:00 +03:00
2018-11-06 11:38:40 +03:00
+------------------+-----------------------------------+------------------------------------------------+
| Type | Example | Meaning |
+==================+===================================+================================================+
| `` one-to-one `` | `` author `` and `` passport_info `` | - an `` author `` can have one `` passport_info `` |
| | | - a `` passport_info `` can have one `` owner `` |
+------------------+-----------------------------------+------------------------------------------------+
| `` one-to-many `` | `` author `` and `` article `` | - an `` author `` can have many `` articles `` |
| | | - an `` article `` can have one `` author `` |
+------------------+-----------------------------------+------------------------------------------------+
| `` many-to-many `` | `` article `` and `` tag `` | - an `` article `` can have many `` tags `` |
| | | - a `` tag `` can have many `` articles `` |
+------------------+-----------------------------------+------------------------------------------------+
2018-09-11 14:11:24 +03:00
2018-12-03 15:12:24 +03:00
GraphQL schema relationships
----------------------------
2018-11-06 11:38:40 +03:00
As you can see, each table relationship will have two component relationships (one in either direction) in the GraphQL
schema. These relationships can be one of the following types:
2018-09-11 14:11:24 +03:00
2018-11-06 11:38:40 +03:00
+-----------------------------------------+------------------------------------------+---------------------------------------------------------------------------------------+
| Type | Example | Meaning |
+=========================================+==========================================+=======================================================================================+
| `` object relationship `` (one-to-one) | an `` article `` can have one `` author `` | an `` article `` object will have a single nested author object called `` author `` |
+-----------------------------------------+------------------------------------------+---------------------------------------------------------------------------------------+
| `` array relationship `` (one-to-many) | an `` author `` can have many `` articles `` | an `` author `` object will have an array of nested article objects called `` articles `` |
+-----------------------------------------+------------------------------------------+---------------------------------------------------------------------------------------+
2018-09-11 14:11:24 +03:00
2018-11-06 11:38:40 +03:00
.. note ::
2018-10-18 17:27:00 +03:00
2018-11-06 11:38:40 +03:00
The relationship name is used to refer to the nested objects in queries. For example, "`` articles `` " of an `` author ``
and "`` author `` " of an `` article `` .
2018-10-18 17:27:00 +03:00
2018-12-03 15:12:24 +03:00
Managing GraphQL relationships
------------------------------
2018-11-06 11:38:40 +03:00
See the following to manage the object/array relationships between tables/views for the GraphQL schema:
2018-10-18 17:27:00 +03:00
2020-03-11 22:42:36 +03:00
- :ref: `create_relationships`
- :ref: `rename_relationships`
2018-10-18 17:27:00 +03:00
2018-12-03 15:12:24 +03:00
Table relationships modelling guides
------------------------------------
2018-11-06 11:38:40 +03:00
The following guides will help you model the different types of table relationships in the database:
2018-10-18 17:27:00 +03:00
2020-03-11 22:42:36 +03:00
- :ref: `one_to_one_modelling`
- :ref: `one_to_many_modelling`
- :ref: `many_to_many_modelling`
2018-09-12 12:28:55 +03:00
2018-11-06 11:38:40 +03:00
.. toctree ::
:maxdepth: 1
:hidden:
2018-09-12 12:28:55 +03:00
2018-11-06 11:38:40 +03:00
create
rename
2020-01-14 15:57:45 +03:00
Database modelling <database-modelling/index>