I am testing locally with MySQL and deploying to heroku ( postgress ) The following query seems to
generate a PGError.
SELECT COUNT(*) FROM “events” INNER JOIN “events” “events_2” WHERE (“events”.“schedule_id” = 1 AND “events”.“start_at” = “events_2”.“start_at” AND “events_2”.“schedule_id” IN (3))
I am testing locally with MySQL and deploying to heroku ( postgress ) The following query seems to
generate a PGError.
SELECT COUNT(*) FROM “events” INNER JOIN “events” “events_2” WHERE (“events”.“schedule_id” = 1 AND “events”.“start_at” = “events_2”.“start_at” AND “events_2”.“schedule_id” IN (3))
Brad, I would recommend trying the following if you haven’t already done so:
Writing the code without accessing the underlying ARel structure.
Running the raw query within a PG client.
I haven’t played enough with ARel but I would suggest trying (1) and (2) above. Futhermore, by doing (1), you can see what SQL is being generated by using the
SELECT COUNT(*) FROM “events” INNER JOIN “events” “events_2” WHERE (“events”.“schedule_id” = 1 AND “events”.“start_at” = “events_2”.“start_at” AND “events_2”.“schedule_id” IN (3));
is bad but this is ok
SELECT COUNT(*) FROM “events” INNER JOIN “events” “events_2” ON (“events”.“schedule_id” = 1 AND “events”.“start_at” = “events_2”.“start_at” AND “events_2”.“schedule_id” IN (3));
Perhaps in postgres using a JOIN always requires an ON clause though in MySQL this is not always the case.
It looks like MySQL is forgiving the missing ON in your first expression thus executing your WHERE clause as a normal WHERE condition of the JOIN. PostgreSQL is not forgiving you the missing ON statement in the join. It’s looking for that ON because it knows you’re running a JOIN and the SQL syntax states you need an ON. Not all databases follow or enforce the same SQL rules so SQL code written for one will not port nicely over to the other all the time.