aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-09-09 15:17:46 +0200
committerYves Senn <yves.senn@gmail.com>2014-09-09 15:17:46 +0200
commitfde0d0219d844bcf71d8f6d9c9420fb75cf9ece9 (patch)
tree35296d699d7c3ade61581645a1001a6dc3dcffcf /activerecord
parentec6eee5db0994f92ac2c3c6d2a922c6edd35b2b1 (diff)
downloadrails-fde0d0219d844bcf71d8f6d9c9420fb75cf9ece9.tar.gz
rails-fde0d0219d844bcf71d8f6d9c9420fb75cf9ece9.tar.bz2
rails-fde0d0219d844bcf71d8f6d9c9420fb75cf9ece9.zip
models backed by views don't assume "id" columns are the primary key.
Closes #10247. The same goes for tables with an "id" column but without primary key constraint. Reading from the view works without configuration. If you have an updateable view you need to use `self.primary_key = ''`.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/test/cases/view_test.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb
index 2f23d12de5..c2e71612a3 100644
--- a/activerecord/test/cases/view_test.rb
+++ b/activerecord/test/cases/view_test.rb
@@ -42,6 +42,13 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase
assert_equal({"id" => 2, "name" => "Ruby for Rails", "status" => 0},
Ebook.first.attributes)
end
+
+ def test_does_not_assume_id_column_as_primary_key
+ model = Class.new(ActiveRecord::Base) do
+ self.table_name = "ebooks"
+ end
+ assert_nil model.primary_key
+ end
end
class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase
@@ -80,5 +87,9 @@ class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase
assert_equal({"name" => "Agile Web Development with Rails", "status" => 0},
Paperback.first.attributes)
end
+
+ def test_does_not_have_a_primary_key
+ assert_nil Paperback.primary_key
+ end
end
end