diff options
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 27 |
4 files changed, 38 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d2ee1faedd..be8765366c 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Migrations: create_table supports primary_key_prefix_type. #10314 [student, thechrisoshow] + * Added logging for dependency load errors with fixtures #11056 [stuthulhu] * Time zone aware attributes use Time#in_time_zone [Geoff Buesing] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 4bd06d54e9..4531c12110 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -974,14 +974,19 @@ module ActiveRecord #:nodoc: end def reset_primary_key #:nodoc: + key = get_primary_key(base_class.name) + set_primary_key(key) + key + end + + def get_primary_key(base_name) #:nodoc: key = 'id' case primary_key_prefix_type when :table_name - key = Inflector.foreign_key(base_class.name, false) + key = Inflector.foreign_key(base_name, false) when :table_name_with_underscore - key = Inflector.foreign_key(base_class.name) + key = Inflector.foreign_key(base_name) end - set_primary_key(key) key end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index b471b153de..b1f1fd4d73 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -89,7 +89,7 @@ module ActiveRecord # See also TableDefinition#column for details on how to create columns. def create_table(table_name, options = {}) table_definition = TableDefinition.new(self) - table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false + table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false yield table_definition diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 5dbfbd81c2..ebe563059d 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -181,6 +181,33 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.drop_table :testings rescue nil end + def test_create_table_with_primary_key_prefix_as_table_name_with_underscore + ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore + + Person.connection.create_table :testings do |t| + t.column :foo, :string + end + + assert_equal %w(foo testings_id), Person.connection.columns(:testings).map { |c| c.name }.sort + ensure + Person.connection.drop_table :testings rescue nil + ActiveRecord::Base.primary_key_prefix_type = nil + end + + def test_create_table_with_primary_key_prefix_as_table_name + ActiveRecord::Base.primary_key_prefix_type = :table_name + + Person.connection.create_table :testings do |t| + t.column :foo, :string + end + + assert_equal %w(foo testingsid), Person.connection.columns(:testings).map { |c| c.name }.sort + ensure + Person.connection.drop_table :testings rescue nil + ActiveRecord::Base.primary_key_prefix_type = nil + end + + # SQL Server, Sybase, and SQLite3 will not allow you to add a NOT NULL # column to a table without a default value. unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :SQLiteAdapter) |