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

collection_func.count is not working #22443

Closed
abdonrd opened this issue May 9, 2024 · 8 comments · Fixed by #22448
Closed

collection_func.count is not working #22443

abdonrd opened this issue May 9, 2024 · 8 comments · Fixed by #22448

Comments

@abdonrd
Copy link
Contributor

abdonrd commented May 9, 2024

Describe the Bug

From Directus v10.11.0, collection_func.count has stopped working.

With a GraphQL query like this (each battle has a list of videos):

{
  battles {
    id
    videos_func { count }
  }
}

I get this error:

{
  "errors": [
    {
      "message": "An unexpected error occurred.",
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR"
      }
    }
  ]
}

And if I remove the videos_func { count } it works.

It was working before update to v10.11.0.

To Reproduce

Create a GraphqQL query with _func.count.

Directus Version

v10.11.0

Hosting Strategy

Self-Hosted (Docker Image)

@hanneskuettner
Copy link
Contributor

hanneskuettner commented May 9, 2024

Heya, I've tried replicating this in v10.11.0 on my end, but so far my GraphQL queries seem to work just find for m2m, m2a and o2m relations.

Can you please share your collection setup, DB vendor and maybe even a stack trace you might be seeing in your Directus logs?

@abdonrd
Copy link
Contributor Author

abdonrd commented May 9, 2024

Right!

With this query:

{
  battles {
    id
    videos_func { count }
  }
}

The response is:

{
  "errors": [
    {
      "message": "An unexpected error occurred.",
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR"
      }
    }
  ]
}

And the error is:

ERROR: select "battles"."id", (select count(*) from "videos" as "kaggh" left join "matchdays" as "jjvhv" on "videos"."matchday" = "jjvhv"."id" left join "battles" as "xcswd" on "videos"."battle" = "xcswd"."id" where "kaggh"."battle" = "battles"."id" and ((("jjvhv"."status" = 'published' or "xcswd"."status" = 'published')))) AS "videos_count" from "battles" where (("battles"."status" = $1)) order by "battles"."id" asc limit $2 - invalid reference to FROM-clause entry for table "videos"

From:

ERROR: select "battles"."id", (select count(*) from "videos" as "kaggh" left join "matchdays" as "jjvhv" on "videos"."matchday" = "jjvhv"."id" left join "battles" as "xcswd" on "videos"."battle" = "xcswd"."id" where "kaggh"."battle" = "battles"."id" and ((("jjvhv"."status" = 'published' or "xcswd"."status" = 'published')))) AS "videos_count" from "battles" where (("battles"."status" = $1)) order by "battles"."id" asc limit $2 - invalid reference to FROM-clause entry for table "videos"
  err: {
    "type": "GraphQLError",
    "message": "select \"battles\".\"id\", (select count(*) from \"videos\" as \"kaggh\" left join \"matchdays\" as \"jjvhv\" on \"videos\".\"matchday\" = \"jjvhv\".\"id\" left join \"battles\" as \"xcswd\" on \"videos\".\"battle\" = \"xcswd\".\"id\" where \"kaggh\".\"battle\" = \"battles\".\"id\" and (((\"jjvhv\".\"status\" = 'published' or \"xcswd\".\"status\" = 'published')))) AS \"videos_count\" from \"battles\" where ((\"battles\".\"status\" = $1)) order by \"battles\".\"id\" asc limit $2 - invalid reference to FROM-clause entry for table \"videos\"",
    "stack":
      error: select "battles"."id", (select count(*) from "videos" as "kaggh" left join "matchdays" as "jjvhv" on "videos"."matchday" = "jjvhv"."id" left join "battles" as "xcswd" on "videos"."battle" = "xcswd"."id" where "kaggh"."battle" = "battles"."id" and ((("jjvhv"."status" = 'published' or "xcswd"."status" = 'published')))) AS "videos_count" from "battles" where (("battles"."status" = $1)) order by "battles"."id" asc limit $2 - invalid reference to FROM-clause entry for table "videos"
        at Parser.parseErrorMessage (/directus/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/dist/parser.js:283:98)
        at Parser.handlePacket (/directus/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/dist/parser.js:122:29)
        at Parser.parse (/directus/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/dist/parser.js:35:38)
        at Socket.<anonymous> (/directus/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/dist/index.js:11:42)
        at Socket.emit (node:events:517:28)
        at addChunk (node:internal/streams/readable:368:12)
        at readableAddChunk (node:internal/streams/readable:341:9)
        at Readable.push (node:internal/streams/readable:278:10)
        at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)
    "path": [
      "battles"
    ],
    "locations": [
      {
        "line": 2,
        "column": 3
      }
    ],
    "extensions": {}
  }

Using PostgreSQL v15.

And the collection is (both matchday & video are M2O):

Screenshot 2024-05-09 at 7 03 21 AM

@hanneskuettner
Copy link
Contributor

Are you seeing this in the app or when querying the API? Because the DB query looks like some additional filters are applied?
It would help if you could share the whole request. Thank you!

@abdonrd
Copy link
Contributor Author

abdonrd commented May 9, 2024

Just with the query:

POST /graphql

Payload:

{"query":"{\n  battles {\n    id\n    videos_func { count }\n  }\n}","variables":null}

@hanneskuettner
Copy link
Contributor

Just to double check, you're not using any custom hooks or flows that might transform that query before it hits the DB?

@abdonrd
Copy link
Contributor Author

abdonrd commented May 9, 2024

Yes, no custom hooks / flows related to this query.

@hanneskuettner
Copy link
Contributor

hanneskuettner commented May 9, 2024

The missing piece to reproducing this were the Item Permissions for the videos collection. With this filter I can reproduce the error. And this looks like a regression caused in #22297.

{
	"_and": [{
		"_or": [{
			"battle": {
				"status": {
					"_eq": "published"
				}
			}
		}, {
			"matchday": {
				"status": {
					"_eq": "published"
				}
			}
		}]
	}]
}

@abdonrd
Copy link
Contributor Author

abdonrd commented May 9, 2024

Right, in my matchdays and battle collections I have the status field. And in the permission I required to published.

The videos has no status field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants