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.

:table_name_with_underscore behavior is inconsistent between migrations and find.

#872

Greetings,

In schema_statements.rb#create_table it defines the primary key using the table name as the base name.

If we posit an accounts table, using :table_name_with_underscore, this means it will create a primary key of (for example) accounts_id.

When actually referencing that table from an ActiveRecord object, via Account.find(1) it will fail, because it is trying to use a primary key based off the class name, which is singular, and so will do a select for account_id.

I believe the singular is what most folks would expect the table name to come out as (accounts table has an account_id as the primary key), but one way or another the two portions are inconsistent.

Here's a shot at a patch, including fixes for the two migrations tests that use that feature. One thing I'm not sure of is if this patch needs to take into account the table pluralization setting.

I'm throwing this over to Jeremy Kemper, as the last person to touch the line I've changed in create_table. My apologies if that's the wrong person.

-- Morgan Schweers

Reported by Morgan Schweers · August 20th, 2008 @ 08:04 PM

State: committed
Milestone: 2.3.4
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. josh
    josh
    • State changed from new to stale

    Staling out, let me know if its still an issue.

    November 22nd, 2008 @ 07:02 PM

  2. Todd Hanson
    Todd Hanson
    • Title changed from :table_name_with_underscore behavior is inconsistent between migrations and find. to still an issue

    both 2.1.2 and 2.2.2 are affected

    February 20th, 2009 @ 09:26 PM

  3. Todd Hanson
    Todd Hanson
    • Title changed from still an issue to :table_name_with_underscore behavior is inconsistent between migrations and find.

    sorry, didn't mean to change the title.

    February 20th, 2009 @ 09:27 PM

  4. josh
    josh
    • State changed from stale to open

    February 20th, 2009 @ 10:31 PM

  5. Mike Breen
    Mike Breen
    • Tag changed from 2.1, activerecord, bug, edge, patch to 2.1, activerecord, bug, bugmash, edge, patch

    +1 verified that the patch applies cleanly to 2-3-stable. The supplied tests will fail without the included changes.

    August 10th, 2009 @ 12:49 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Tag changed from 2.1, activerecord, bug, bugmash, edge, patch to 2.1, activerecord, bug, edge, patch
    • Milestone changed from 2.x to 2.3.4

    August 10th, 2009 @ 05:59 AM

  7. Repository
    Repository
    • State changed from open to committed

    (from [d0bdff0799a4e6c6752fca56e69fd8b529117768]) Fix that creating a table whose primary key prefix type is :table_name generates an incorrectly pluralized primary key.

    [#872 :table_name_with_underscore behavior is inconsistent between migrations and f... state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/d0bdff0799a4e6c6752fca56e69fd8...

    August 10th, 2009 @ 06:01 AM

  8. Repository
    Repository

    (from [d2d464e26e9d41b7299675e145ae1455cce37dd4]) Fix that creating a table whose primary key prefix type is :table_name generates an incorrectly pluralized primary key.

    [#872 :table_name_with_underscore behavior is inconsistent between migrations and f... state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/d2d464e26e9d41b7299675e145ae14...

    August 10th, 2009 @ 06:01 AM