Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding properties to entities named "<entity name>Id" cause issues when nesting relationship fields #18085

Closed
cody-fjorge opened this issue Sep 15, 2023 · 9 comments · Fixed by #20027
Labels
issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product source: core:strapi Source is core/strapi package status: confirmed Confirmed by a Strapi Team member or multiple community members version: 4 Issue related to v4 version: 5

Comments

@cody-fjorge
Copy link

cody-fjorge commented Sep 15, 2023

Bug report

Required System information

  • Node.js version: 16.6.1
  • NPM version: 8.2.0
  • Strapi version: 4.9.2
  • Database: Mysql 2.18.1
  • Operating system: mac/windows/linux
  • Is your project Javascript or Typescript: Javascript

Describe the bug

Adding a property to an entity with the name of "Id" that also has a relationship property to the same entity, when getting results from the api if you try to read the value of Id of the parent from inside the child the value will be the row ID of the child.

Steps to reproduce the behavior

  • Create an entity named 'shirt'
  • Add a text prop named 'shirtId'
  • Save
  • Add a one to many relational field to shirt named variants for the many and variantOf for the one.
  • Create a shirt with a shirtId = 'abc'
  • Create a shirt with a shirtId = 'xyz'
  • Add the second shirt as a variant of the first shirt

When pulling results from the api:

  • The parent shirt's shirtId = "abc" and ID of 1
  • The parent has a variant shirt with a shirtId of "xyz" and ID of 2
  • The variant shirt has a variantOf property to reference it's parent
  • The variant shirt's variantOf shirt will have a shirtId of "2" (the ID of the variant) and an ID of 1

Expected behavior

The Id field should be correct when looking at the parent data from the child

Additional context

If this shirtId property is changed to something like "shirtUid" the bug does not happen.

@NmN03jain
Copy link

can you pls suggest the path or files in which i have to make changes ?

@Boegie19 Boegie19 added the status: can not reproduce Not enough information to reproduce label Oct 3, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

This is a templated message

Hello @cody-fjorge,

Thank you for reporting this bug, however we are unable to reproduce the issue you described given the information we have on hand. Can you please create a fresh project that you are able to reproduce the issue in, provide clear steps to reproduce this issue, and either upload this fresh project to a new GitHub repo or compress it into a .zip and upload it on this issue?

We would greatly appreciate your assistance with this, by working in a fresh project it will cut out any possible variables that might be unrelated.
Please note that issues labeled with status: can not reproduce will be closed in 14 days if there is no activity.

Thank you!

@Boegie19
Copy link
Collaborator

Boegie19 commented Oct 3, 2023

Tryed to reproduce the issue but I got the output like I expected

{
   "data":[
      {
         "id":1,
         "attributes":{
            "A":"A",
            "createdAt":"2023-10-03T17:58:22.854Z",
            "updatedAt":"2023-10-03T17:58:37.884Z",
            "publishedAt":"2023-10-03T17:58:37.880Z",
            "Parent":{
               "data":[
                  
               ]
            },
            "Child":{
               "data":[
                  {
                     "id":2,
                     "attributes":{
                        "A":"B",
                        "createdAt":"2023-10-03T17:58:29.948Z",
                        "updatedAt":"2023-10-03T17:58:40.387Z",
                        "publishedAt":"2023-10-03T17:58:40.385Z"
                     }
                  }
               ]
            }
         }
      },
      {
         "id":2,
         "attributes":{
            "A":"B",
            "createdAt":"2023-10-03T17:58:29.948Z",
            "updatedAt":"2023-10-03T17:58:40.387Z",
            "publishedAt":"2023-10-03T17:58:40.385Z",
            "Parent":{
               "data":[
                  {
                     "id":1,
                     "attributes":{
                        "A":"A",
                        "createdAt":"2023-10-03T17:58:22.854Z",
                        "updatedAt":"2023-10-03T17:58:37.884Z",
                        "publishedAt":"2023-10-03T17:58:37.880Z"
                     }
                  }
               ]
            },
            "Child":{
               "data":[
                  
               ]
            }
         }
      }
   ],
   "meta":{
      "pagination":{
         "page":1,
         "pageSize":25,
         "pageCount":1,
         "total":2
      }
   }
}

@jason-Impress
Copy link

Getting this issue too. My attribute is "formId" and the REST API only ever returns "1" for the value when it's retrieved through a relationship in a dynamic zone component.

@Boegie19
Copy link
Collaborator

Boegie19 commented Oct 4, 2023

Getting this issue too. My attribute is "formId" and the REST API only ever returns "1" for the value when it's retrieved through a relationship in a dynamic zone component.

Mind giving me the content type like you use it with replication staps?

@cody-fjorge
Copy link
Author

Tryed to reproduce the issue but I got the output like I expected

{
   "data":[
      {
         "id":1,
         "attributes":{
            "A":"A",
            "createdAt":"2023-10-03T17:58:22.854Z",
            "updatedAt":"2023-10-03T17:58:37.884Z",
            "publishedAt":"2023-10-03T17:58:37.880Z",
            "Parent":{
               "data":[
                  
               ]
            },
            "Child":{
               "data":[
                  {
                     "id":2,
                     "attributes":{
                        "A":"B",
                        "createdAt":"2023-10-03T17:58:29.948Z",
                        "updatedAt":"2023-10-03T17:58:40.387Z",
                        "publishedAt":"2023-10-03T17:58:40.385Z"
                     }
                  }
               ]
            }
         }
      },
      {
         "id":2,
         "attributes":{
            "A":"B",
            "createdAt":"2023-10-03T17:58:29.948Z",
            "updatedAt":"2023-10-03T17:58:40.387Z",
            "publishedAt":"2023-10-03T17:58:40.385Z",
            "Parent":{
               "data":[
                  {
                     "id":1,
                     "attributes":{
                        "A":"A",
                        "createdAt":"2023-10-03T17:58:22.854Z",
                        "updatedAt":"2023-10-03T17:58:37.884Z",
                        "publishedAt":"2023-10-03T17:58:37.880Z"
                     }
                  }
               ]
            },
            "Child":{
               "data":[
                  
               ]
            }
         }
      }
   ],
   "meta":{
      "pagination":{
         "page":1,
         "pageSize":25,
         "pageCount":1,
         "total":2
      }
   }
}

This looks like you didn't setup the fields correctly from my reproduction steps since there is no field named Id (ex. shirtId if the entity is named shirt).

@Boegie19
Copy link
Collaborator

Boegie19 commented Oct 4, 2023

I see now I am closing it since there is a tracking issue for this. #16222

@Boegie19 Boegie19 closed this as completed Oct 4, 2023
@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product status: confirmed Confirmed by a Strapi Team member or multiple community members source: core:strapi Source is core/strapi package and removed status: can not reproduce Not enough information to reproduce labels Mar 12, 2024
@derrickmehaffy
Copy link
Member

I'm able to reproduce it on v4.20.4.

Here is the content-type schema I used:

{
  "kind": "collectionType",
  "collectionName": "shirts",
  "info": {
    "singularName": "shirt",
    "pluralName": "shirts",
    "displayName": "shirt",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "shirtId": {
      "type": "string"
    },
    "variants": {
      "type": "relation",
      "relation": "oneToMany",
      "target": "api::shirt.shirt",
      "mappedBy": "variantOf"
    },
    "variantOf": {
      "type": "relation",
      "relation": "manyToOne",
      "target": "api::shirt.shirt",
      "inversedBy": "variants"
    }
  }
}

You need two entries with the relation set on one of them:

image
image

Here is the request you need to make: localhost:1337/api/shirts?populate[variants][populate]=*

Response payload:

image

@derrickmehaffy
Copy link
Member

Tested on 5.0.0-beta.0 and the issue is still there but the odd thing is it only appears at a deeper than 1 nesting level.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product source: core:strapi Source is core/strapi package status: confirmed Confirmed by a Strapi Team member or multiple community members version: 4 Issue related to v4 version: 5
Projects
Status: Fixed/Shipped
Archived in project
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants