Possible to insert object with many-to-many relation in one query?
See original GitHub issueI 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 tag
s.
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:
- Created 4 years ago
- Reactions:6
- Comments:17 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@marionschleifer Reopening this to track the docs enhancements. We should add an example for many-to-many relationship in nested inserts.
@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 theupdate_columns
field. In case of nested upserts like our case here, if you pass theupdate_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 calledvalue
)with response
Hope this clarifies things