Skip to content
Advertisement

How I can update one to many relation in Postgres?

Here is my first table question.

Questions have many options, so I referring every option row with question_id

Now, How can I update both tables in one query?

I building an API. The below-given output is for request. The request will response with question details and options for the question.

I am able to update question but questions have many options, How can I update options?

Now Can I update this relation?

Advertisement

Answer

You can chain multiple operations together in a single query by using CTEs that have returning clauses.

The same thing can be done for insert and delete statements.

Do note you actually need to select from the CTE in the following query, otherwise the statement within will not be executed.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement