This project is archived and is in readonly mode.
postgres adapter quotes table name, breaks when non-default schema is used
-
Jeremy Kemper
- State changed from new to open
- Assigned user set 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
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
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
- Tag set 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.
-
Sean Bowman
Whoops...those numbers are supposed to be comments!
-
Jeremy Kemper
- Milestone cleared.
- State changed from open to incomplete
See the mysql adapter for a quick quoting fix for qualified table names.
Could you do a patch + test for this?
-
Sean Bowman
Yes, but it might not be for a week or two.
-
coutinho (at mondriantecnologia)
- Tag changed from 2.1, activerecord, bug, patch, tested to 2.1, active, activerecord, bug, patch, plugin, postgresql, record, tested
Hello Jeremy, I have a plugin to fix this problem in jruby. My plugin can be adapted to fix the problem in ruby. http://coutinho.mondriantecnolog...
-
Max Lapshin
- Tag changed from 2.1, active, activerecord, bug, patch, plugin, postgresql, record, tested to 2.3-rc1, active, activerecord, bug, patch, plugin, postgresql, record, tested
- Assigned user changed from Jeremy Kemper to Tarmo Tänav
I've added patch, that makes possible to define following table_names:
table_name "table.name" schema_name.table_name schema_name."table.name" "schema.name".table_name "schema.name"."table.name"
-
Tarmo Tänav
- State changed from incomplete to open
-
Max Lapshin
Posting patch second time. Now all test over activerecord seems to run. Appended test for proper quoting
-
Elan Feingold
The patch works great over here, would love to see it in the next release of Rails.
-
Max Lapshin
Thanks. I've also integrated it into several projects and use it.
-
Pete Deffendol
+! - the patch is working great over here. I just dropped it into config/initializers.
-
Repository
- State changed from open to resolved
(from [70de8e64e30092b2b1b77869e459b4868f5b9577]) Support multiple schemas in table names for postgresql [#390 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
-
Repository
(from [60601234708b34c81dcd6a58a0cad79a7520ce10]) Support multiple schemas in table names for postgresql [#390 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
