Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7763 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Ensure primary key column is always quoted

Reported by: toolmantim Assigned to: bitsweat
Priority: high Milestone: 1.2.3
Component: ActiveRecord Version: edge
Severity: normal Keywords: postgres primary key quoting
Cc:

Description

ActiveRecord::Base is inconsistent with quoting of primary key columns:

Member.find(1)
# => SELECT * FROM members WHERE (members.id = 1)
Member.find_by_id(1)
# => SELECT * FROM members WHERE (members."id" = 1) LIMIT 1

This has bad side-effects when using mixed-case primary keys on case-sensitive databases, such as PostgreSQL.

Ticket #4547 originally brought this up but failed to provide a decent patch, though it looked like bitsweat was waiting for one so he could commit the fix.

Attached is a patch which ensure all references to the primary key are column quoted by the connection adapater.

AR tests pass in MySQL and PostgreSQL.

No tests are included with the patch, because a) there's no tests for AR's orginal behaviour, 2) how on earth do I add a test for this?

The following test would work on PostgreSQL because it throws a SQL error that it can't find the column "primarykey", but is no good for other adapters, and doesn't directly test if AR::Base is generating SQL with the pk column quoted.

CREATE TABLE mixed_case_primary_keys ("primaryKey" INTEGER PRIMARY KEY);
INSERT INTO TABLE mixed_case_primary_key VALUES (1);

class MixedCasePrimaryKey < ActiveRecord::Base
  set_primary_key "primaryKey"
end

class MixedCasePrimaryKeyTest < Test::Unit::Case
  def test_should_search_primary_keys
    assert_nothing_raised do
      MixedCasePrimaryKey.find(1)
    end
  end
end

Attachments

active_record_quote_primary_key_column.patch (14.4 kB) - added by toolmantim on 03/09/07 01:10:01.
New patch on trunk with tests
active_record_quote_primary_key_column_1_2_stable.patch (14.8 kB) - added by toolmantim on 03/09/07 01:10:31.
New patch on stable branch with tests

Change History

03/08/07 10:00:59 changed by bitsweat

  • cc deleted.
  • keywords set to postgres primary key quoting.

Looks good; thanks. In general, please attach fixes to the original ticket rather than creating new ones. Trac is enough of a jungle as is ;)

03/08/07 11:50:27 changed by toolmantim

no problemo.

03/08/07 21:41:51 changed by toolmantim

Ok I can probably infer each adapter's quoting style from looking at their quote_column_name method, and update all the relevant SQL files and add a test for this case. Will do so and report back.

03/09/07 01:10:01 changed by toolmantim

  • attachment active_record_quote_primary_key_column.patch added.

New patch on trunk with tests

03/09/07 01:10:31 changed by toolmantim

  • attachment active_record_quote_primary_key_column_1_2_stable.patch added.

New patch on stable branch with tests

03/09/07 01:13:27 changed by toolmantim

I've updated all the SQL adapter definitions (tested MySQL & PostgreSQL) and added tests covering the previous fixes (+1 extra fix which the tests uncovered).

03/09/07 03:25:57 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.
  • milestone changed from 1.x to 1.2.3.

[6364] trunk, [6365] 1-2-stable. Thanks for testing!