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

Ambiguous column "id" when PG COUNT is used with certain joins #1112

Open
njradford opened this issue Oct 17, 2023 · 1 comment
Open

Ambiguous column "id" when PG COUNT is used with certain joins #1112

njradford opened this issue Oct 17, 2023 · 1 comment

Comments

@njradford
Copy link

njradford commented Oct 17, 2023

When attempting to get the COUNT somewhere within the kaminari code, an ambiguous column error is being generated:

PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous

Screenshot 2023-10-17 at 12 56 10 PM

This is the generated SQL that's throwing the error.

SELECT 
  COUNT(DISTINCT id) 
FROM 
  (
    (
      SELECT 
        "initiatives".* 
      FROM 
        "initiatives" 
        LEFT OUTER JOIN "initiative_collaborator_junctions" ON "initiative_collaborator_junctions"."subject_type" = $1 
        AND "initiative_collaborator_junctions"."initiative_id" = "initiatives"."id" 
        LEFT OUTER JOIN "users" ON "users"."id" = "initiative_collaborator_junctions"."subject_id" 
      WHERE 
        "users"."id" = 71
    ) 
    UNION 
      (
        SELECT 
          "initiatives".* 
        FROM 
          "initiatives" 
          LEFT OUTER JOIN "initiative_partners" ON "initiative_partners"."initiative_id" = "initiatives"."id" 
          LEFT OUTER JOIN "partners" ON "partners"."id" = "initiative_partners"."partner_id" 
        WHERE 
          (
            "partners"."kinds" ~ '(?:^|,|\|)nunya'
          )
      )
  ) "initiatives" 
  INNER JOIN "initiative_partners" ON "initiative_partners"."initiative_id" = "initiatives"."id" 
  INNER JOIN "partners" ON "partners"."id" = "initiative_partners"."partner_id" 
  LEFT OUTER JOIN "focus_area_junctions" ON "focus_area_junctions"."subject_type" = $2 
  AND "focus_area_junctions"."kind" = $3 
  AND "focus_area_junctions"."subject_id" = "initiatives"."id" 
  LEFT OUTER JOIN "focus_areas" ON "focus_areas"."id" = "focus_area_junctions"."focus_area_id" 
WHERE 
  "partners"."kinds" = $4 
  AND "focus_areas"."id" = 'empl

Versions

gem 'kaminari', '~> 1.2', '>= 1.2.2'
gem 'rails', '~> 7.0.3'
gem 'pg', '~> 1.1'

Let me know if I can provide any more details!

@njradford
Copy link
Author

Possibly related to #887

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant