strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable and developer-first.
https://strapi.io
Other
62.79k stars 7.88k forks source link

Warning: Internal Server Error on Click of Publish using a nested component in a collection type #16893

Open de-shine opened 1 year ago

de-shine commented 1 year ago

We are getting below error on console while trying to publish a collection type having a dynamic zone as one of its field type, inside which we use a component carousel (i.e. containing another child component 'Image' within it).

Component Detail and folder Structure: Inside /src/components/content carousel.json:

{
  "collectionName": "components_content_carousels",
  "info": {
    "displayName": "carousel",
    "description": ""
  },
  "options": {},
  "attributes": {
    "items": {
      "type": "component",
      "repeatable": true,
      "component": "content.image",
      "required": true,
      "max": 3
    }
  }
}

image.json:

{
  "collectionName": "components_content_images",
  "info": {
    "displayName": "Image",
    "icon": "image",
    "description": ""
  },
  "options": {},
  "attributes": {
    "title": {
      "type": "string"
    },
    "image": {
      "type": "media",
      "multiple": false,
      "required": false,
      "allowedTypes": [
        "images"
      ]
    },
    "tagline": {
      "type": "text"
    }
  }
}

Steps to reproduce the behavior

-If we use above content.carousel component in a dynamic zone field type of a collection type field , we encountered this error with the below log.

error log:

error: select "t1"."entity_id", count(*) AS count from "components_content_images" as "t0" left join "components_content_carousels_components" as "t1" on "t0"."id" = "t1"."component_id" and "t1"."field" = $1 where ("t0"."publishedAt" is null and "t1"."entity_id" in ($2)) group by "t1"."entity_id" - column t0.publishedAt does not exist
error: select "t1"."entity_id", count(*) AS count from "components_content_images" as "t0" left join "components_content_carousels_components" as "t1" on "t0"."id" = "t1"."component_id" and "t1"."field" = $1 where ("t0"."publishedAt" is null and "t1"."entity_id" in ($2)) group by "t1"."entity_id" - column t0.publishedAt does not exist
    at Parser.parseErrorMessage (C:\project_project\Code Base\web\strapi-project-backend\node_modules\pg-protocol\dist\parser.js:287:98)
    at Parser.handlePacket (C:\project_project\Code Base\web\strapi-project-backend\node_modules\pg-protocol\dist\parser.js:126:29)
    at Parser.parse (C:\project_project\Code Base\web\strapi-project-backend\node_modules\pg-protocol\dist\parser.js:39:38)

Test cases: Scenario 1 : - If the collection type is already in published state - No ERROR- It publish successfully. Scenario 2 : - If the collection type is not published yet- We get ERROR (see logs above). Publish fails.

Required System information

Kindly help in fixing this.

giacomoforlani commented 1 year ago

I have the same problem, so I try to add more details.

I investigated deeply and I discovered that the bug is caused by the endpoint /content-manager/single-types/api::homepage.homepage/actions/numberOfDraftRelations, called before the publish action. The method that throws the exception is this one.

To reproduce the bug, it's necessary to have a content type (single or collection) with the following schema:

{
    "kind": "singleType",
    "collectionName": "examples",
    "info": {
        "singularName": "example",
        "pluralName": "examples",
        "displayName": "Example",
    },
    "options": {
        "draftAndPublish": true
    },
    "pluginOptions": {},
    "attributes": {
        "body": {
            "type": "dynamiczone",
            "components": [
                "test.component-one",
                "test.component-two",
            ]
        }
    }
}

The schema for test.component-one is:

{
    "collectionName": "components_unit_banner_authors",
    "info": {
        "displayName": "Banner Author",
        "description": ""
    },
    "options": {},
    "attributes": {
        "item": {
            "type": "component",
            "component": "test.component-three",
        }
    }
}

And the schema for test.component-two is:

{
    "collectionName": "components_unit_banner_authors",
    "info": {
        "displayName": "Banner Author",
        "description": ""
    },
    "options": {},
    "attributes": {
        "item": {
            "type": "relation",
            "relation": "oneToOne",
            "target": "api::relation.relation",
        }
    }
}

Both components have the field item but

The method linked above generates query filters for each field of the dynamic zone but in that case is merging them because they have the same name. The final result returns a filter on the publishedAt: { $null: true } for the items field. The query fails while checking if there is any draft relation for test.component-one because the field is not a relation.

The issue is similar to another one already resolved: https://github.com/strapi/strapi/issues/11955

I hope that this comment is clear and can be useful for the resolution.