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

Table names don't always get properly quoted #1631

Closed
jasonswett opened this issue May 4, 2013 · 4 comments · Fixed by #3652
Closed

Table names don't always get properly quoted #1631

jasonswett opened this issue May 4, 2013 · 4 comments · Fixed by #3652
Assignees

Comments

@jasonswett
Copy link

In lib/rails_admin/config/actions/dashboard.rb, on line 30, it produces an SQL query that doesn't quote the table name referenced (SELECT "user".* FROM "user" ORDER BY user.updated_at desc). This is a problem if you have a table name like "user", which is a reserved word in PostgreSQL.

Here's a gist with a stack trace: https://gist.github.com/jasonswett/1da12e239266f20b17c2

I wrote a fix for this, but this would be my first open source contribution ever, so I might need a little hand-holding to get it in. I understand I'm supposed to write a failing spec, but I don't see a spec for dashboard.rb in what I would assume to be the logical place, spec/rails_admin/config/actions. If someone could help me find that, that would be appreciated.

@neilang
Copy link
Contributor

neilang commented May 16, 2013

If your fix is applied to dashboard.rb, I would just add the missing spec file and test there. You can always update a pull request if a maintainer comments that they would like something changed ;)

@ohararyan
Copy link

Looks like this issue still exists as I've got the same problem currently. Is there any update on this?

@mshibuya mshibuya removed this from the 1.1.0 milestone Jul 17, 2018
@rnestler
Copy link
Contributor

I also just hit that issue.

@rnestler
Copy link
Contributor

rnestler commented Oct 13, 2023

I found the root cause: If not quoted postgres will downcase everything, so if I have a column name "FOO_BAR" and try to query it like SELECT "foo".* FROM "fo" ORDER BY foo.FOO_BAR desc it will be converted to SELECT "foo".* FROM "fo" ORDER BY foo.foo_bar desc which then won't work.

See https://stackoverflow.com/a/2878408/3497181

Postgresql treats identifiers case insensitively when not quoted (it actually folds them to lowercase internally), and case sensitively when quoted; many people are not aware of this idiosyncrasy.

Rails / ActiveModel itself does the correct thing, so if I execute Foo.order(:FOO_BAR) it generates the correctly quoted SQL.

Edit: Actually ActiveModel does only the right thing when passing the column name as symbol. If I pass it as 'FOO_BAR' it will also fail.

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

Successfully merging a pull request may close this issue.

6 participants