directus / v8-archive

Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API
https://docs.directus.io/api/reference.html
507 stars 203 forks source link

Unable to save some m2m fields #2192

Open reddo opened 4 years ago

reddo commented 4 years ago

Directus API version 8.6.2 PHP version 7.3 MySQL version 10.2.27-MariaDB-cll-lve Apache version 2.2.34

I have a collection with a bunch of many-to-many fields. All of the many-to-many fields have exactly the same configuration. I edit the collection, add values to the m2m fields and some of them throw the following error: Error: Attempting to write an array as the value for field "family_features" in "locations_family_features" The payload looks right too:

{
  "cuisine": [
    {
      "cuisine": {
        "id": 2
      }
    },
    {
      "cuisine": {
        "id": 3
      }
    }
  ],
  "family_features": [
    {
      "family_features": {
        "id": 2
      }
    },
    {
      "family_features": {
        "id": 6
      }
    }
  ],
  "features": [
    {
      "features": {
        "id": 6
      }
    }
  ]
}

The features and family_features throw the error. All of the junction collections were auto-generated and look exactly the same to me.

reddo commented 4 years ago

I went through the database and I couldn't find any difference between the 2 entities and their relation (collections, junction collections, and fields are all basically the same). I am stumped.

I even deleted and recreated the fields and junction collections. Any ideas as to what else I could try?

rijkvanzanten commented 4 years ago

What are the fields in the current, junction, and related fields?

reddo commented 4 years ago

@rijkvanzanten, I'm not sure which collections' field you need, but here's what I got that's related Main collections' m2m field: image junction collection: image Related collection: image

rijkvanzanten commented 4 years ago

Thanks! That's exactly what I was looking for. I'm wondering if it has something to do with the fact that the name of the o2m is the same as the name in the junction table 🤔 That shouldn't cause it to break of course, but that's one explanation why some m2ms fail, but others don't.

reddo commented 4 years ago

It's the same for the rest of the m2m fields too. I can try and use different names. It's really inconvenient that I can't edit m2m fields though.

reddo commented 4 years ago

@rijkvanzanten, using different names for the junction collection fields seems to work. Now I have to redo the other fields. I'm curious as to why it worked well with the other fields though.

rijkvanzanten commented 4 years ago

Yeah me too! I'm starting to suspect that it might be the API mishandling it, as the payload in the OP looks correct to me 🤔