Nested Inserts (Joins)

Joins in Insert Queries

While inserts traditionally target a single table, QueryDeck allows you to create related records in nested structures, effectively simulating joins during data insertion. This approach is particularly useful for maintaining parent-child relationships, such as creating a customer and their associated rental records in a single request.

Nested Inserts

When inserting data with relationships, you can use nested objects to represent the joined tables.

Creating a join in an insert query

First, select the 'join' button on the left side pane

in the pop that appears, choose tables to join to the selected parent table (here we have chosen rental and payment tables) and close the pop up.

next copy the URL of the endpoint to test by clicking on the top right area

Save the created endpoint, by clicking on "save"

Testing the endpoint created

Open postman, choose the method as post and paste the url in the input bar

Then click on body, choose type as raw(json), and use the request body, required as per your need. Here we are using the following body to insert a customer with rentals and payments informations

Request Body:

{
  "customer": [
    {
      "store_id": 1,
      "first_name": "Jared",
      "last_name": "Ely",
      "address_id": 530,
      "active": 1,
      "activebool": true,
      "create_date": "2006-02-14T00:00:00.000Z",
      "email": "[email protected]",
      "last_update": "2013-05-26T14:49:45.738Z",
      "rental": [
        {
          "staff_id": 1,
          "rental_date": "2023-01-01T12:00:00.000Z",
          "inventory_id": 1,
          "payment": [
            {
              "payment_date": "2023-01-01T12:00:00.000Z",
              "customer_id": 524,
              "staff_id": 1,
              "amount": 12.34
            }
          ]
        }
      ]
    }
  ]
}

Explanation

  • customer: Represents the parent table.

  • rental: Represents the child table, included as a nested array.

  • payment: Represents the grandchild table, nested under rental.

This structure ensures that all related records are inserted in a single operation, preserving the relationships between the tables.

Now click on send near the input bar the send the request

You should get the following response

Last updated