Skip to content

ActiveRecord::QueryMethods.group() doesn't work if it's a MySQL keyword #15008

@arp242

Description

@arp242

ActiveRecord::QueryMethods.group() throws a MySQL error if you're grouping by an SQL keyword (in this case: key).

>> Template.group :key
!! #<ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key' at line 1: SELECT `templates`.* FROM `templates`  GROUP BY key>

The problem is that the SQL statement needs backticks around the column name.

A workaround which works (but will break some operations like .count())

Template.order '`key`'

Other operations (.where(), .order(), etc.) work as expected.

$ rails --version
Rails 4.1.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions