Permalink
Browse files

Migrations: create_table supports primary_key_prefix_type. Closes #10314

.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9056 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 856a4dc commit dfa68883113045aad4bfab87c82b54d0c98d0a89 @jeremy jeremy committed Mar 18, 2008
View
@@ -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]
@@ -974,14 +974,19 @@ def primary_key
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
@@ -89,7 +89,7 @@ def columns(table_name, name = nil) end
# 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
@@ -181,6 +181,33 @@ def test_create_table_with_limits
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)

0 comments on commit dfa6888

Please sign in to comment.