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

Changeset 9056

Show
Ignore:
Timestamp:
03/18/08 18:23:14 (6 months ago)
Author:
bitsweat
Message:

Migrations: create_table supports primary_key_prefix_type. Closes #10314.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9050 r9056  
    11*SVN* 
     2 
     3* Migrations: create_table supports primary_key_prefix_type.  #10314 [student, thechrisoshow] 
    24 
    35* Added logging for dependency load errors with fixtures #11056 [stuthulhu] 
  • trunk/activerecord/lib/active_record/base.rb

    r9023 r9056  
    975975 
    976976      def reset_primary_key #:nodoc: 
     977        key = get_primary_key(base_class.name) 
     978        set_primary_key(key) 
     979        key 
     980      end 
     981 
     982      def get_primary_key(base_name) #:nodoc: 
    977983        key = 'id' 
    978984        case primary_key_prefix_type 
    979985          when :table_name 
    980             key = Inflector.foreign_key(base_class.name, false) 
     986            key = Inflector.foreign_key(base_name, false) 
    981987          when :table_name_with_underscore 
    982             key = Inflector.foreign_key(base_class.name) 
    983         end 
    984         set_primary_key(key) 
     988            key = Inflector.foreign_key(base_name) 
     989        end 
    985990        key 
    986991      end 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    r9014 r9056  
    9090      def create_table(table_name, options = {}) 
    9191        table_definition = TableDefinition.new(self) 
    92         table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false 
     92        table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false 
    9393 
    9494        yield table_definition 
  • trunk/activerecord/test/cases/migration_test.rb

    r9003 r9056  
    181181      Person.connection.drop_table :testings rescue nil 
    182182    end 
     183 
     184    def test_create_table_with_primary_key_prefix_as_table_name_with_underscore 
     185      ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore 
     186 
     187      Person.connection.create_table :testings do |t| 
     188          t.column :foo, :string 
     189      end 
     190 
     191      assert_equal %w(foo testings_id), Person.connection.columns(:testings).map { |c| c.name }.sort 
     192    ensure 
     193      Person.connection.drop_table :testings rescue nil 
     194      ActiveRecord::Base.primary_key_prefix_type = nil 
     195    end 
     196 
     197    def test_create_table_with_primary_key_prefix_as_table_name 
     198      ActiveRecord::Base.primary_key_prefix_type = :table_name 
     199 
     200      Person.connection.create_table :testings do |t| 
     201          t.column :foo, :string 
     202      end 
     203 
     204      assert_equal %w(foo testingsid), Person.connection.columns(:testings).map { |c| c.name }.sort 
     205    ensure 
     206      Person.connection.drop_table :testings rescue nil 
     207      ActiveRecord::Base.primary_key_prefix_type = nil 
     208    end 
     209 
    183210 
    184211    # SQL Server, Sybase, and SQLite3 will not allow you to add a NOT NULL