A relational insert
Having a (simplified) model with a many to many relation like so
| customers | |
|---|---|
| id | uuid |
| first_name | text |
| last_name | text |
| banks | |
|---|---|
| id | uuid |
| name | text |
| customer_bank_accounts | |
|---|---|
| account_number | bigint |
| customer_id | uuid |
| bank_id | uuid |
Now lets assume that in my application I have a form that lets you add a new customer. On that form, besides creating a new customer I also want to immediately assign him to a known bank. Of course I could just: (in pseudo code)
newCustomerId = POST Prefer: return=representation /customers?select=id { "first_name": "John", "last_name": "Doe" }POST /customer_bank_accounts { "account_number": 123456788901234556152, "customer_id": ${newCustomerId}, "bank_id": "some_preexisting_id_of_a_bank_that_the_user_selected"
However this of course is not trasactional and the problem here is that if step 2 fails I would like to rollback and discard the creation of a customer. But boom tss, the customer has already been created!
Is there an api that would let me to do this like so?
POST http://localhost:3000/customers?select=*,customer_bank_accounts{account_number, bank_id}
{
"first_name":"John",
"last_name":"Doe",
"customer_bank_accounts":[
{
"account_number":5851859590151,
"bank_id":"some_preexisting_id_of_a_bank_that_the_user_selected",
"customer_id": "$parent_id????"
}
]
}From the docs I figure that the only way to achieve such would be to manually write a procedure and then call it via rpc, would that be correct?
Source: PostgREST/postgrest