postgres adapter quotes table name, breaks when non-default schema is used
Reported by Olek Poplavsky | June 11th, 2008 @ 11:20 AM | in 2.2
Our app is using some tables in non-default schemas. It used to work ok, only thing to do was to set table name manually, like:
class PersistedProperty < ActiveRecord::Base
set_table_name 'common.persisted_properties'
end
It broke with Rails 2.1 because AbscractAdapter now includes module abstract/quoting.rb, and that one defines quoting of table names by default. In postgres, quoting like this 'select * from "bar"' works, so is 'select * from foo."bar"', but 'select * from "foo.bar"' does NOT work.
I monkey patched this issue locally using included code, but it would be nice to have it fixed in the source. Included fix is just a quick workaround, it may be or may not be the code for longer term fix.
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
def quote_table_name(name)
name
end
end
end
end
Comments and changes to this ticket
-
Jeremy Kemper June 11th, 2008 @ 02:40 PM
- → State changed from new to open
- → Assigned user changed from to Jeremy Kemper
Well, removing quoting is a 'fix' unless you need a quoted table name :) How about smartening up quoting to recognize schema-qualified table names? Or introducing
self.table_schema = 'common' self.table_name = 'persisted_properties' -
Olek Poplavsky June 16th, 2008 @ 05:17 PM
Hi Jeremy
It was nice seeing you at RailsConf :)
The syntax you are offering is nice and I would love to have it, but it would require significant reworking of all of the database adapters, and I am not quite in position to do so, especially because I am only using PostgreSQL adapter. I am just trying to be pragmatic and do things the easiest possible way :)
BTW - I have found 2 more problems in Rails 2.1 with non-default schema support.
One was that column name escaping was really interfering with the way query for auto incrementing primary keys was generated. Again, I used simplest possible solution at hand - disabled column name escaping:
def quote_column_name(name)
name
end
Another problem was that rails could not create indexes on columns in tables with non-default schema. I had to make that method a bit smarter about schemas using simple regexp:
module ActiveRecord
module ConnectionAdapters
module SchemaStatements
def index_name(table_name, options) #:nodoc:
if Hash === options # legacy support
if options[:column]
"index_#{table_name.sub(/^.*\./, '')}_on_#{Array(options[:column]) * '_and_'}"
elsif options[:name]
options[:name]
else
raise ArgumentError, "You must specify the index name"
end
else
index_name(table_name, :column => options)
end
end
end
end
end
Sorry for late reply, got sick on my way back from RailsConf, and it took quite some time to recover...
-

Torben Wölm June 18th, 2008 @ 04:32 PM
Hi
The Oracle adapter has the same problem. But here the quoting is only activated if you use uppercase letters in the table name.
For instance
set_table_name 'myschema.MY_TABLE_NAME'
will trigger the quoting. If you just write the table name in all lowercase, no quoting is done.
This is only a problem with Rails 2.1 for some reason, even though the quoting logic in the adapter hasn't changed.
Are anyone working on general handling of schemas in ActiveRecord -- or is it on a todo list somewhere?
-

Sean Bowman August 5th, 2008 @ 11:27 PM
- → Tag changed from to 2.1 activerecord bug patch tested
Try creating a lib/ file or a plugin with this in the init.rb (we have our models in their own plugin, to share between admin and public apps):
require 'active_record/connection_adapters/postgresql_adapter'
module FixedQuoting
- This is broken in the Rails 2.1.0 PostgreSQL adapter. Doesn't take schema names into account.
- This method fixes this...
def quote_table_name(table_name)
(table_name.split(/\./, 2).map { |piece| %("#{piece}") }).join('.')
end
end
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, FixedQuoting)
This seemed to work for me. At least my DB migrations work now.
-

-
Jeremy Kemper August 28th, 2008 @ 01:24 AM
- → State changed from open to incomplete
- → Milestone changed from to 2.2
See the mysql adapter for a quick quoting fix for qualified table names.
Could you do a patch + test for this?
-

Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
