question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to insert object with many-to-many relation in one query?

See original GitHub issue

I already have many tag objects defined. I have a tag_to_post many-to-many table defined. A user creates a post, sets the title, body, etc. and tags it with 3 tags.

If I insert the post and make 3 insertions into the tag_to_post table in the same query…it can’t work because the tag_to_post insert mutation requires the primary key (id say) of the new post object.

Is there any way to access the last inserted primary key (like RETURNING id) in the same graphql mutation that inserted it?

The only option I see is abandoning Postgres-generated primary keys (auto inc ids, series, etc) and managing random uuids on the app end.

It could be split into 2 queries but then it loses transactionality.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
rikinsk-zzcommented, Jul 9, 2019

@marionschleifer Reopening this to track the docs enhancements. We should add an example for many-to-many relationship in nested inserts.

4reactions
rikinsk-zzcommented, Jun 7, 2019

@msmedes I am not sure I completely understand your use case. But on_conflict seems to be what you are looking for. on_conflict does not cancel a mutation as you mentioned but lets you decide what the action should be based on the update_columns field. In case of nested upserts like our case here, if you pass the update_column field as [<conflict-column>] the insert should go through as expected.

The following is the query I would run to insert a post along with its tags (given tag has a unique field called value)

mutation {
  insert_post(
    objects: [
      {
        title: "new post",
        post_tags: {
          data: [
            {
              tag: {
                data: {
                  value: "tech"
                },
                on_conflict: {
                  constraint: tag_value_key,
                  update_columns: [value]
                }
              }
            },
            {
              tag: {
                data: {
                  value: "blog"
                }
                on_conflict: {
                  constraint: tag_value_key,
                  update_columns: [value]
                }
              }
            }
          ]
        }
      }
    ]
  ) {
    returning {
      id
      title
      post_tags {
        id
        tag {
          id
          value
        }
      }
    }
  }
}

with response

{
  "data": {
    "insert_post": {
      "returning": [
        {
          "id": "f03bdccf-6344-4771-851b-efdd9bc5f4fc",
          "title": "new post",
          "post_tags": [
            {
              "tag": {
                "value": "tech",
                "id": "5e8f15fe-c187-4961-940e-d45206a7ff05"
              },
              "id": "99694d90-245b-4157-9496-a630b563ebd7"
            },
            {
              "tag": {
                "value": "blog",
                "id": "dedd495b-fb2c-4b7f-8989-73b67908352d"
              },
              "id": "fa3d372c-2102-40a2-8892-54bb8cd93e58"
            }
          ]
        }
      ]
    }
  }
}

Hope this clarifies things

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add multiple objects to ManyToMany relationship at ...
Use: object.m2mfield.add(*items) as described in the documentation: add() accepts an arbitrary number of arguments, not a list of them.
Read more >
Create a Many-to-Many Relationship - Salesforce Help
You can use master-detail relationships to model many-to-many relationships between any two objects. A many-to-many relationship allows each record of one ...
Read more >
Many-to-many relationships - Django documentation
To define a many-to-many relationship, use ManyToManyField . In this example, an Article can be published in multiple Publication objects, and a Publication ......
Read more >
Django ORM - Insert - Working with many to many relationships
Django ORM - Insert data into a single table with a many to many relationship · Our journey to master Jetpack Compose •...
Read more >
The right way to use a ManyToManyField in Django
To maintain a many-to-many relationship between two tables in a database, the only way is to have a third table which has references...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found