How to insert data into two tables using one API call? #10034
-
|
I want to create a GraphQL mutation that inserts data into two tables: Table 1 (Main Table) and Table 2 (History Table). The columns in both tables are identical. In Table 1, the primary key is an auto-generated integer. In Table 2, the primary key is also an auto-generated integer, and there's a foreign key that references the primary key value from Table 1. My need is to have a single GraphQL API call that inserts data into both tables, with the foreign key value in Table 2 dependent on the primary key value generated during the insertion into Table 1. How can this be achieved in a GraphQL query? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hello @jvvinoth In order to perform single API to insert data into two tables, there are some prerequisites to follow:
How to create relationships between tables in Hasura - https://hasura.io/docs/latest/schema/postgres/table-relationships/create/ In my case I have authors and articles table where, I have I have created many-to-one relationship (Object based) from articles -> author and one-to-many (Array based) relationship from authors -> articles. After all these above steps are completed, we can prepare nested insert mutation. We have already covered on how to insert an object with its related object(s) through relationships here in Hasura doc - https://hasura.io/docs/latest/mutations/postgres/insert/#pg-nested-inserts Coming back to my example, if I want to insert author and it’s related article in one API call here’s how it would look Query And here’s how my query variables would look like This would give me response like I hope you got the gist of what I am trying to explain. Feel free to drop your concerns/feedbacks. |
Beta Was this translation helpful? Give feedback.
Hello @jvvinoth
In order to perform single API to insert data into two tables, there are some prerequisites to follow:
How to create relationships between tables in Hasura - https://hasura.io/docs/latest/schema/postgres/table-relationships/create/
In my case I have authors and articles table where, I have
idwith UUID type in “authors” table andcreated_bycolumn with UUID type in “art…