Permalink
Browse files

`ActiveRecord::SchemaMigration` has no primary key.

Before this patch, using `ActiveRecord::Base.primary_key_prefix_type`
with `:table_name_with_underscore` would change the `SchemaMigration` model
to have a primary key. This resulted in broken queries for PG because it tried
to return the inserted PK (which does not exist).

Closes #15051.
Closes #15419.

Conflicts:
	activerecord/lib/active_record/schema_migration.rb
  • Loading branch information...
1 parent a9b9b01 commit f5a24bec0cf9645bf75d678b7c6940b527d689b0 @senny senny committed Jun 6, 2014
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* `ActiveRecord::SchemaMigration` has no primary key regardless of the
+ `primary_key_prefix_type` configuration.
+
+ Fixes #15051.
+
+ *JoseLuis Torres*, *Yves Senn*
+
* `rake db:migrate:status` works with legacy migration numbers like `00018_xyz.rb`.
Fixes #15538.
View
3 activerecord/lib/active_record/schema_migration.rb
@@ -4,6 +4,9 @@
module ActiveRecord
class SchemaMigration < ActiveRecord::Base
+ def self.primary_key
+ nil
+ end
def self.table_name
"#{Base.table_name_prefix}schema_migrations#{Base.table_name_suffix}"
View
14 activerecord/test/cases/ar_schema_test.rb
@@ -17,6 +17,20 @@ def teardown
ActiveRecord::SchemaMigration.delete_all rescue nil
end
+ def test_has_no_primary_key
+ old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
+ ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
+ assert_nil ActiveRecord::SchemaMigration.primary_key
+
+ ActiveRecord::SchemaMigration.create_table
+ assert_difference "ActiveRecord::SchemaMigration.count", 1 do
+ ActiveRecord::SchemaMigration.create version: 12
+ end
+ ensure
+ ActiveRecord::SchemaMigration.drop_table
+ ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
+ end
+
def test_schema_define
ActiveRecord::Schema.define(:version => 7) do
create_table :fruits do |t|

0 comments on commit f5a24be

Please sign in to comment.