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

PG::UndefinedParameter: ERROR: there is no parameter $1 #59

Open
jyr opened this issue Sep 29, 2016 · 7 comments
Open

PG::UndefinedParameter: ERROR: there is no parameter $1 #59

jyr opened this issue Sep 29, 2016 · 7 comments

Comments

@jyr
Copy link

jyr commented Sep 29, 2016

Hi!

I have of next versions:

  • Rails 4.1.1
  • postgres_ext (2.4.1)
  • postgres_ext-serializers (0.0.3)
  • active_model_serializers (0.8.3)

my query is:

@model1.model2.includes(:model3).where('model3.state =?', 'done')

I have a error when run:

json = ActiveModel::ArraySerializer.new(ts, each_serializer: Api::V1::MySerializer).to_json

or

json = @model1.model2.includes(:model3).where('model3.state =?', 'done')
render json: json, each_serializer: Api::V1::MySerializer

Could you help me?

Thanks a lot!

@felixbuenemann
Copy link
Collaborator

Please try with master, I think this is already fixed.

@jyr
Copy link
Author

jyr commented Sep 29, 2016

@felixbuenemann Hi,

I tried with:

gem 'postgres_ext-serializers', git: 'https://github.com/DockYard/postgres_ext-serializers.git'

the new version is:

postgres_ext-serializers (0.0.3 5de27f2)

I have the same error:

PG::UndefinedParameter: ERROR:  there is no parameter $1
LINE 1: ..." = "table1"."id" WHERE "table1"."attribute_id" = $1 AND (bo...

@felixbuenemann
Copy link
Collaborator

felixbuenemann commented Sep 29, 2016

It's hard to say with all the anonymised queries, but have you tried this?

@model1.model2.joins(:model3).where("model3.state = 'done'")

You don't really need a prepared statement for a static string.

Another possible solution using a subquery:

model3_ids = Model3.where(state: 'done').select(:id)
@model1.model2.where(model3_id: model3_ids)

The subquery approach is often faster than a join.

@jyr
Copy link
Author

jyr commented Sep 29, 2016

Not works, I have the same error:

ActiveRecord::StatementInvalid (PG::UndefinedParameter: ERROR: there is no parameter $1

I think is error is of rails 4, the sql result is:

LINE 1: ..." = "model3"."id" WHERE "model3"."model1_id" = $1 AND "ti

@felixbuenemann
Copy link
Collaborator

Can you post the result of calling to_sql on your relation?

It looks like you have some code that is trying to do preloading and none of the examples I posted above would trigger that.

@jyr
Copy link
Author

jyr commented Sep 30, 2016

the result of sql:

"SELECT \"tickets\".* FROM \"tickets\" INNER JOIN \"bookings\" ON \"tickets\".\"booking_id\" = \"bookings\".\"id\" WHERE \"bookings\".\"event_id\" = $1 AND (bookings.state ='complete')"

and query

event.tickets.includes(:booking).where('bookings.state =?', 'done')

and the result of json query when I used a serializer

(WITH tickets_attributes_filter AS (SELECT "tickets"."number", "tickets"."consecutive_number", "tickets"."ticket_type_name", "tickets"."email", "tickets"."first_name", "tickets"."last_name", "tickets"."checked_in" FROM "tickets" INNER JOIN "bookings" ON "tickets"."booking_id" = "bookings"."id" WHERE "bookings"."event_id" = $1 AND (booking_id in (SELECT "bookings"."id" FROM "bookings" WHERE "bookings"."state" = 'complete') and event_id =2173)), tickets_as_json_array AS (SELECT COALESCE(json_agg(tbl), '[]') as tickets, 1 as match FROM (SELECT * FROM "tickets_attributes_filter") AS tbl), jsons AS (SELECT "tickets_as_json_array"."tickets" FROM "tickets_as_json_array") SELECT row_to_json(jsons) FROM "jsons")

@felixbuenemann
Copy link
Collaborator

Ah, that should be easy to fix. Try Ticket.where(event: event).…

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

2 participants