This project is archived and is in readonly mode.
PostgreSQL schema dumper does not support capitalized table names
The postgresql adapter appears to have a bug that causes a PGError to be raised if there are any tables in the database that start with a capital letter.
This seems to be caused by the fact that in the SQL schema queries, the table name is cast to type regclass without using double quotes, so the case of the table name is not preserved. This causes the cast to fail if the table name starts with or contains capital letters.
For example, if there is a table "CamelCase" in the database, then the schema dumper will fail with the following error:
ActiveRecord::StatementInvalid: PGError: ERROR: relation "camelcase" does not exist : SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = 'CamelCase'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
The attached patch does the following:
- Introduces a capitalized table name into the test schema and reads the table name back to verify that the existing code works for MySQL and SQLite, but breaks for PostgreSQL.
- Adds quotes around the table name when casting to type regclass in the postgresql adapter.
- Improves postgresql table name quoting so that it quotes properly in the case of a prefixed schema as well (the schema should not be included in the quotes).
- Adds a test to the PostgreSQL schema test to verify that this works whether or not a schema is specified before the table name, e.g. test_schema.TableName
- ActiveRecord 2.3.2
- PostgreSQL 8.2.11
- "pg" gem 0.8.0
- MacOS 10.5 Leopard
Comments and changes to this ticket
I had to revise my patch a bit, since the patch from #390 got committed yesterday, which implemented even more complete table-name quoting than I had.
So the new patch does the following:
- Quotes the table name when casting to regclass so that capitalized table names are supported. Does so using the new quote_table_name method from #390.
- Adds tests for capitalized table names, including when the occur with prefixed schema names (the purpose of #390).
I've rebased against master and consolidated into a single diff.
- Tag changed from activerecord, adapters, camelcase, patch, postgresql, schema, tables to activerecord, adapters, bug, camelcase, patch, postgresql, schema, tables, verified
- Assigned user set to Tarmo Tänav
I was afraid, that this code will fail, when declaring table in other schema, but it worked for me! Checked against post-2.3.2 master
P.S. Am I right, changing its state to verified?
- State changed from new to resolved
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>