activerecord-hierarchical_query: ActiveRecord::StatementInvalid errors with v1.0.0 and Rails 5

Hi,

I’m trying to use v1.0.0 with Rails 5, and my queries don’t quite work. I could really use some help.

This does what is expected (gets the ancestors as a relation):

relation = self.class.join_recursive do |query|
  query.start_with(id: id).connect_by(parent_organization_id: :id)
end

and if I call #to_a on that, I get the correct results. However, if I do any of these things:

relation.first, relation.where(...), relation.limit(...), relation.order(..) I get an error like this:

ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR:  bind message supplies 1 parameters, but prepared statement "a3" requires 2

Here’s an example of the generated SQL when it fails to on relation.first:

SELECT "organizations".* FROM "organizations"
INNER JOIN
  (WITH RECURSIVE "organizations__recursive" AS
     (SELECT "organizations"."id", "organizations"."parent_organization_id"
      FROM "organizations"
      WHERE "organizations"."deleted_at" IS NULL
        AND "organizations"."id" = $1
        UNION ALL
        SELECT "organizations"."id",
               "organizations"."parent_organization_id"
        FROM "organizations"
        INNER JOIN "organizations__recursive" ON "organizations__recursive"."parent_organization_id" = "organizations"."id" WHERE "organizations"."deleted_at" IS NULL ) SELECT "organizations__recursive".*
   FROM "organizations__recursive") AS "organizations__recursive" ON "organizations"."id" = "organizations__recursive"."id"
WHERE "organizations"."deleted_at" IS NULL
ORDER BY "organizations"."id" ASC
LIMIT $2

Any ideas? Thanks in advance!

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25

Most upvoted comments

Sounds fair. It is going to take me a little longer to reproduce again as I’m fixing some conflicts in the branch I was working in. I’ll re-open the issue when it gets to that point.

Is this still an issue in Rails 5.1? I’m going to help keep this project maintained and I want to fix what’s broken and make sure we can support Rails 5.2 now that it is out.