Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

PostgreSQL schema dumper does not support capitalized table names

#2418

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

My environment:

  • ActiveRecord 2.3.2
  • PostgreSQL 8.2.11
  • "pg" gem 0.8.0
  • MacOS 10.5 Leopard

Reported by Scott Woods · April 5th, 2009 @ 12:35 AM

State: resolved
Milestone: 2.x
Assigned to: Tarmo Tänav Tarmo Tänav
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski

    Can you rebase these down into a single patch, and ideally someone with the postgres gem can verify it. I'm also pg gem on 10.5

    April 5th, 2009 @ 03:57 AM

  2. Scott Woods
    Scott Woods

    I had to revise my patch a bit, since the patch from #390 postgres adapter quotes table name, breaks when non-default schema is used got committed yesterday, which implemented even more complete table-name quoting than I had.

    So the new patch does the following:

    I've rebased against master and consolidated into a single diff.

    April 6th, 2009 @ 03:23 AM

  3. Scott Woods
    Scott Woods

    Verified against the postgres (0.7.9.2008.01.28) gem.

    • MacOS 10.5 Leopard
    • PostgreSQL 8.2.11

    April 6th, 2009 @ 03:28 AM

  4. clinton
    clinton
    1. I ran all tests successfully, against the master. Code makes sense.

    Mac OS X 1.5, PostgreSQL 8.3.6.

    April 7th, 2009 @ 06:28 PM

  5. Max Lapshin
    Max Lapshin
    • 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?

    April 20th, 2009 @ 05:16 PM

  6. Repository
    Repository
    • State changed from new to resolved

    (from [64b33b6cf9db508d2c12394cc1a3f36c91fb2eed]) Quote table names when casting to regclass so that capitalized tables are supported. [#2418 PostgreSQL schema dumper does not support capitalized table names state:resolved]

    Signed-off-by: Tarmo Tänav tarmo@itech.ee http://github.com/rails/rails/co...

    April 21st, 2009 @ 11:51 AM

  7. Repository
    Repository

    (from [70ba90b072025b89248606178ee30d2ff12301c4]) Quote table names when casting to regclass so that capitalized tables are supported. [#2418 PostgreSQL schema dumper does not support capitalized table names state:resolved]

    Signed-off-by: Tarmo Tänav tarmo@itech.ee http://github.com/rails/rails/co...

    April 21st, 2009 @ 11:52 AM