Skip to content
Browse files

Fixed that create table with :id => false and fixtures don't play nic…

…e together (closes #10154) [jbarnette]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8205 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent ea713b6 commit f4b5ca1ff0113e41c974b7cbda1f734f9c0526fe @dhh dhh committed Nov 25, 2007
View
8 activerecord/lib/active_record/fixtures.rb
@@ -574,8 +574,8 @@ def insert_fixtures
row[key] = label if value == "$LABEL"
end
- # generate a primary key
- row[primary_key_name] = Fixtures.identify(label)
+ # generate a primary key if necessary
+ row[primary_key_name] = Fixtures.identify(label) if has_primary_key?
model_class.reflect_on_all_associations.each do |association|
case association.macro
@@ -633,6 +633,10 @@ def primary_key_name
@primary_key_name ||= model_class && model_class.primary_key
end
+ def has_primary_key?
+ model_class && model_class.columns.any? { |c| c.name == primary_key_name }
+ end
+
def timestamp_column_names
@timestamp_column_names ||= %w(created_at created_on updated_at updated_on).select do |name|
column_names.include?(name)
View
6 activerecord/test/fixtures/db_definitions/schema.rb
@@ -335,4 +335,10 @@ def create_table(*args, &block)
t.column :parrot_id, :integer
t.column :treasure_id, :integer
end
+
+ create_table :mateys, :id => false, :force => true do |t|
+ t.column :pirate_id, :integer
+ t.column :target_id, :integer
+ t.column :weight, :integer
+ end
end
View
4 activerecord/test/fixtures/matey.rb
@@ -0,0 +1,4 @@
+class Matey < ActiveRecord::Base
+ belongs_to :pirate
+ belongs_to :target, :class_name => 'Pirate', :foreign_key => 'target_id'
+end
View
4 activerecord/test/fixtures/mateys.yml
@@ -0,0 +1,4 @@
+blackbeard_to_redbeard:
+ pirate_id: <%= Fixtures.identify(:blackbeard) %>
+ target_id: <%= Fixtures.identify(:redbeard) %>
+ weight: 10
View
9 activerecord/test/fixtures_test.rb
@@ -10,6 +10,7 @@
require 'fixtures/parrot'
require 'fixtures/pirate'
require 'fixtures/treasure'
+require 'fixtures/matey'
class FixturesTest < Test::Unit::TestCase
self.use_instantiated_fixtures = true
@@ -451,7 +452,7 @@ def test_cache
end
class FoxyFixturesTest < Test::Unit::TestCase
- fixtures :parrots, :parrots_pirates, :pirates, :treasures
+ fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys
def test_identifies_strings
assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo"))
@@ -533,6 +534,12 @@ def test_supports_polymorphic_belongs_to
assert_equal(pirates(:redbeard), treasures(:sapphire).looter)
assert_equal(parrots(:louis), treasures(:ruby).looter)
end
+
+ def test_only_generates_a_pk_if_necessary
+ m = Matey.find(:first)
+ m.pirate = pirates(:blackbeard)
+ m.target = pirates(:redbeard)
+ end
end
class ActiveSupportSubclassWithFixturesTest < ActiveSupport::TestCase

0 comments on commit f4b5ca1

Please sign in to comment.
Something went wrong with that request. Please try again.