From fde0d0219d844bcf71d8f6d9c9420fb75cf9ece9 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 15:17:46 +0200 Subject: 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 = ''`. --- activerecord/test/cases/view_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord') 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 -- cgit v1.2.3