Replies: 5 comments 10 replies
-
A many to many is nothing more than a one-to-many to a junction table, with a many-to-one from there to the related collection. Creating an item in a many to one would be: {
"first_name": "Frank",
"last_name": "Alistar",
"courses": [
{
"course_id": 3
}
]
} (assuming the field in your junction table is called |
Beta Was this translation helpful? Give feedback.
-
while this isssue is not yet resolved, I am using an intermediate solution by making an additional api call and writing both id's directly into the junction table to create the relation. (in case someone else has a similar problem...) |
Beta Was this translation helpful? Give feedback.
-
Hi @martinval I have faced the same issue and i figured out how to consume the API with M2M relationship field. In my case, I have collection post that have many category, and the category can be for many post. So, the correct curl is:
where category, is the name of database field in post collection. category_id is the FK. |
Beta Was this translation helpful? Give feedback.
-
Heya, just a notice/warning, if you send multiple times So lets say you you post or patch this, 8 is a fixed id of you relational item
but if you would like to delete its not the same id, e.g.
|
Beta Was this translation helpful? Give feedback.
-
For anyone else trying this out, @abu7elo's answer is correct, however for subsequents calls to create more relations, it will over-write any existing ones so you'd have to make sure to read the existing ones in first, then append the new ones when writing them back. A far easier way that I've been using is simply to create a new item in the junction table, and then the o2m and m2o fields on each collection on either side should update themselves automatically. So for instance, I have two collections, "movies" and "actors". Movies has a "related_actors" m2m field, and vice-versa, Actors has a "related_actors" m2m field. Both of these share the same junction table called "actors_movies". All I need to do is create a new item in "actors_movies" like so:
|
Beta Was this translation helpful? Give feedback.
-
I have a courses table and a participants table linked by a junction table.
In the web interface I can add courses to participants and the other way around, but when I want to create a participant via api post request, the relation is not created.
The docs are quite sparse on explaining M2M relations and item creation....
My json request body looks like this:
[ { "firstname": "frank","lastname": "alistair", "courses": 3 } ]
I've tried "courses": "3" / "courses": [{"id": 3 }] / "courses": [{"id": "3" }] ....either nothing happens or I get a server error.
Thanks in advance for any help
Beta Was this translation helpful? Give feedback.
All reactions