tenzan  
                
                  
                    November 9, 2022, 12:02pm
                   
                  1 
               
             
            
              
System Information 
Strapi Version : 4.4.7 
Operating System : 
Database : 
Node Version : 
NPM Version : 
Yarn Version : 
 
 
I want to create relations between Post and Tag as follows.
Post has many Tags 
Tags have and belongs to many Posts 
 
I created collection types for Post and Tag.
Made them publicly available (for public user)
I created 1 post with 3 tags.
Now, when I try to see them at http://localhost:1337/api/posts 
And lastly, I was not able to create Many-to-Many relations between Post and Tag with the following error:
             
            
              
           
          
            
            
              You need to add the populate parameter to your REST API requests in order for Strapi to populate the relations.
             
            
              1 Like 
            
           
          
            
              
                tenzan  
              
                  
                    November 9, 2022, 11:34pm
                   
                  3 
               
             
            
              Thanks! This worked for Posts.
             
            
              
           
          
            
            
              That’s odd. Can you show your tag’s schema.json ?
             
            
              1 Like 
            
           
          
            
              
                tenzan  
              
                  
                    November 10, 2022, 11:23am
                   
                  5 
               
             
            
              Thanks.
In tag’s schema.json, there’s no relation yet, as I was not able to add in Strapi admin due to error.
{
  "kind": "collectionType",
  "collectionName": "tags",
  "info": {
    "singularName": "tag",
    "pluralName": "tags",
    "displayName": "Tag",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "name": {
      "type": "string",
      "required": true
    }
  }
}
 
            
              
           
          
            
            
              Hmm looks fine to me. How about the post’s schema.json ? Is there already a tags  attribute?
             
            
              
           
          
            
              
                tenzan  
                
                  
                    November 10, 2022, 11:29am
                   
                  7 
               
             
            
              Post’s schema.json looks fine.
{
  "kind": "collectionType",
  "collectionName": "posts",
  "info": {
    "singularName": "post",
    "pluralName": "posts",
    "displayName": "Post",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "title": {
      "type": "string",
      "required": true
    },
    "body": {
      "type": "text",
      "required": true
    },
    "tags": {
      "type": "relation",
      "relation": "oneToMany",
      "target": "api::tag.tag"
    }
  }
}
but I don’t see posts as relation against tag, when accessing http://localhost:1337/api/tags?populate[0]=posts 
In this case, posts and tags supposed to be in many-to-many relations.
             
            
              
           
          
            
              
                tenzan  
                
                  
                    November 10, 2022, 11:30am
                   
                  8 
               
             
            
            
              
           
          
            
            
              Ah, your Post schema already contains a tags  field which is defined as a oneToMany relation to Tag. The attribute names must be unique.
I see two ways you can change this.
1. UI Way 
In the content type builder, remove the existing tags relation field in the post scheme 
save 
Now you’re free to create the many-to-many relation as you intended 
 
2. Manual way 
In your post’s schema.json , change the tags  attribute to: 
 
"tags": {
  "type": "relation",
  "relation": "manyToMany",
  "target": "api::tag.tag",
  "mappedBy": "tags"
}
In your tag’s schema.json , add a posts  attribute: 
 
"posts": {
  "type": "relation",
  "relation": "manyToMany",
  "target": "api::post.post",
  "inversedBy": "posts"
}
EDIT:
             
            
              
           
          
            
              
                tenzan  
              
                  
                    November 10, 2022, 12:16pm
                   
                  10 
               
             
            
              Thanks a lot!
             
            
              1 Like