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

[v5] Cannot populate multiple relations to the same table using the REST API and Document Service API #20330

Open
razvanilin opened this issue May 19, 2024 · 0 comments
Assignees
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: core:core Source is core/core source: core:database Source is core/database package status: pending reproduction Waiting for free time to reproduce the issue, or more information version: 5

Comments

@razvanilin
Copy link

Bug report

  • Node.js version: 20.10.0
  • NPM version: 10.2.3
  • Strapi version: v5-beta.7
  • Database: MySQL
  • Operating system: MacOS
  • Is your project Javascript or Typescript: Javascript

Describe the bug

I have a table that has two different relation fields with the same table. So in table Property I have the following:

  • contracts is a field with the belongs to many relation
  • active_contract is a field with has one relation

The problem is that when I use the REST API and include the populate query param, only the contracts gets populated even though I indicate that I want active_contract too. active_contract always returns as null.

When using the new Document Service API directly in the controller with the same params, active_contract gets populated and contracts is []

The only thing that worked is to write a custom controller for the find and findOne methods to return the results with the Entity Query API. So it seems that in v5, I cannot rely on the REST API directly. This definitely feels like a bug

Steps to reproduce the behavior

  1. Create table property
  2. Create table contract
  3. Add two relation fields to property called contracts (belongs to many) and active_contract (has one)
  4. Enter some dummy data
  5. Try the REST API with the populate query params and it will not return both of the relations

Expected behavior

I want both my relations populated when I make a REST API call or use the new Document Service API

Screenshots

CleanShot 2024-05-19 at 21 20 00@2x

Code snippets

❌ REST API Example on the client that doesn't populate active_contract:

const url = `${API_URL}/properties`;
    const method = "GET";
    const headers = {
      "accept": "application/json",
      "authorization": `Bearer ${getCookie(AUTH_TOKEN)}`,
    };

    try {
      const response = await axios.get(url, {
        method,
        headers,
        params: {
          filters: {
            team: {
              id: { $eq: teamId }
            },
          },
          populate: {
            image: true,
            active_contract: true,
            contracts: {
              populate: ["renters"],
            },
          },
        },
      });

      return response.data;

❌ Document Service API where this time contracts returns an empty array and active_contract is populated:

const entities = await strapi.documents('api::property.property').findMany({
      filters: {
        team: {
          id: { $eq: teamRecord.id },
        },
      },
      populate: {
        image: true,
        contracts: {
          populate: ["renters"],
        },
        active_contract: true,
      },
    });

✅ And the Query Engine API that works:

const entities = await strapi.db.query('api::property.property').findMany({
      where: {
        team: {
          id: { $eq: teamRecord.id },
        },
      },
      populate: {
        image: true,
        contracts: {
          populate: ["renters"],
        },
        active_contract: true,
      },
    });
@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: core:database Source is core/database package status: pending reproduction Waiting for free time to reproduce the issue, or more information version: 5 source: core:core Source is core/core labels May 31, 2024
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: medium If it breaks the basic use of the product but can be worked around source: core:core Source is core/core source: core:database Source is core/database package status: pending reproduction Waiting for free time to reproduce the issue, or more information version: 5
Projects
Status: To be reviewed (Open)
Status: To triage
Status: To review
Development

No branches or pull requests

3 participants