diff --git a/diagrams/users.er b/diagrams/users.er new file mode 100644 index 000000000..e42ca312f --- /dev/null +++ b/diagrams/users.er @@ -0,0 +1,13 @@ +[Users] +*id +first_name +last_name +username + +[Subscriptions] +*+subscriber_id +*+subscribed_id +type + +Users 1--* Subscriptions +Subscriptions *--1 Users diff --git a/docs/_static/users.png b/docs/_static/users.png new file mode 100644 index 000000000..f8b57fee7 Binary files /dev/null and b/docs/_static/users.png differ diff --git a/docs/references/api/resource_embedding.rst b/docs/references/api/resource_embedding.rst index 54de4d460..392655a8d 100644 --- a/docs/references/api/resource_embedding.rst +++ b/docs/references/api/resource_embedding.rst @@ -983,3 +983,41 @@ Hints also work alongside ``!inner`` if a top level filtering is needed. From th If the relationship is so complex that hint disambiguation does not solve it, you can use :ref:`computed_relationships`. +.. _recursive_m2m_disamb: + +Recursive Many-To-Many Disambiguation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Use hints plus the :ref:`spread_embed` to disambiguate a recursive many-to-many relationship. Having the following: + +.. image:: ../../_static/users.png + +Note that ``subscriptions`` has more than one foreign key to ``users``. + +To get all the subscribers of a user: + +.. tabs:: + + .. code-tab:: http + + GET /users?select=username,subscribers:subscriptions!subscribed_id(...users!subscriber_id(username))&id=eq.4 HTTP/1.1 + + .. code-tab:: bash Curl + + curl "http://localhost:3000/users?select=username,subscribers:subscriptions!subscribed_id(...users!subscriber_id(username))&id=eq.4" + +.. code-block:: json + + [ + { + "username": "the_top_artist", + "subscribers": [ + { "username": "patrick109" }, + { "username": "alicia_smith" } + ] + } + ] + +.. note:: + + We're working on a better interface for recursive relationships to reduce the request verbosity.