13

I'm trying to do a POST to the Strapi API and can't seem to figure out how to attach a 'has and belongs to many' (many to many) relationship.

I've already tried the following body's:

events: ["ID", "ID"]
name: "name"

&

events: [ID, ID]
name: "name"

Which regarding the docs should be right, I think.

There's no error, I get a '200 OK' response. It adds the record but without the relations.

Event.settings.json:

{
  "connection": "default",
  "collectionName": "events",
  "info": {
    "name": "event",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": [
      "created_at",
      "updated_at"
    ],
    "comment": ""
  },
  "attributes": {
    "name": {
      "type": "string"
    },
    "artists": {
      "collection": "artist",
      "via": "events",
      "dominant": true
    }
  }
}

Artist.settings.json:

{
  "connection": "default",
  "collectionName": "artists",
  "info": {
    "name": "artist",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": [
      "created_at",
      "updated_at"
    ],
    "comment": ""
  },
  "attributes": {
    "name": {
      "required": true,
      "type": "string"
    },
    "events": {
      "collection": "event",
      "via": "artists"
    }
  }
}

I'm using the standard SQLite database, strapi version 3.0.0-beta.13 and tried the request through Postman, HTML & curl.

I would love to know how to attach the relation on POST

Update 23-07:

Did a fresh install of Strapi and now everything is working.

SalahAdDin
  • 2,023
  • 25
  • 51
Max B
  • 213
  • 2
  • 3
  • 12
  • Hi Max B, `artist` is `string` type and `events` is relation? Did you try add `refId` from `event` to `events` field collection? – Daniel Karski Jul 18 '19 at 11:47
  • You mean like [1, 2]? If so, yes I already tried this. – Max B Jul 18 '19 at 14:20
  • Could you share your correct payload? What is 1 and 2? Is it `Scalars["ID"]` from artist object? – Daniel Karski Jul 19 '19 at 06:14
  • Sorry, 1 and 2 indeed represent the respective ID's of the artist objects. So yes, it's Scalars["ID"] from artist object. – Max B Jul 19 '19 at 06:24
  • ok, could you share error from response? You could edit question. – Daniel Karski Jul 19 '19 at 06:26
  • There's no error, I get a '200 OK' response. It adds the record but without the relations – Max B Jul 19 '19 at 08:01
  • Could you share me yours both `*.setting.json` (ex. `event.setting.json`) by edit question. You can find them in `${APP_NAME}/api/event/models/event.setting.json` – Daniel Karski Jul 19 '19 at 09:18
  • @DanielKarski edited the question with the *.settings.json – Max B Jul 21 '19 at 11:57
  • Could you tried change `collectionName` from `artists` to `artist` and `events` to `event` like is in the nested fields `"collection": "artist"` & `"collection": "event"`. Maybe they do not pair with each other. I avoid naming models in the plural. – Daniel Karski Jul 22 '19 at 06:40
  • Doing that causes an error: ```error Something went wrong in the model `Artist` with the attribute `event` error Error: The attribute `artists` is missing in the model Event``` Also, these models & relationships are made through the Strapi UI. So I think the models & relationships are set up correctly. I also did some tests with different kind of relations. The only relation that isn't working is the 'has and belongs to many'. – Max B Jul 22 '19 at 09:02

3 Answers3

3

I think it's because your set you ID as a String instead of an Integer

{
  events: [1, 2],
  name: "My name"
}

And here 1 and 2 are the IDs of events you want to add.

Jim LAURIE
  • 3,859
  • 1
  • 11
  • 14
  • What is the version of Strapi you are using? And how did you request the API? Postman, HTML/JS form, curl? – Jim LAURIE Jul 22 '19 at 08:19
  • Using Strapi version 3.0.0-beta.13 and tried the request through Postman, HTML & curl. – Max B Jul 22 '19 at 08:58
  • I just created a new application (with Strapi beta.13 and SQLite) created 2 content type (exact same as yours) then I created 2 event entries that have 1 and 2 as ID. Then I execute the POST request to create an event with the JSON object I send you. And it works well. Can you try with a fresh new app - or sent a GitHub repo - because here I can't help you more. – Jim LAURIE Jul 22 '19 at 14:58
  • Did two fresh installs and now everything is working. Really weird, but thanks for the help. – Max B Jul 23 '19 at 07:38
  • Hum weird at all! – Jim LAURIE Jul 24 '19 at 12:02
1

This is (still? again?) a bug in Strapi, see: https://github.com/strapi/strapi/issues/12238 As a workaround you need to add the find-permission to the user / role who is performing the request for the related content type (you want to check first if this is a security issue for your scenario or not - alternatively you might want to try Paratron's approach which is described in the comments).

dpoerschke
  • 68
  • 7
  • Lol. Thank you. It's my case. I need to create A which relates to B and C. I only grant users access to A. Now I need to grant `find` permission to access B and C in order to create A properly. – Lam Le Mar 12 '23 at 10:38
0

Late reply. Hoping this might help someone!

Right now I am using Strapi v4.3.2 and was facing the same issue. I overcame this by overriding the default core controller for create as explained in official docs. Relations are now visible!

async create(ctx) {
 const { data } = ctx.request.body;

 const response = await strapi.entityService.create(
    "api::collection.collection",
    {
      data: data,
    }
  );

 return {response}
}
  • 1
    Not sure of this answers the question above, but I want to thank you, because this led me down a path to return ID's of a relation when doing a rest api "update". Thank you! – stldoug Mar 31 '23 at 02:19