aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xactiverecord/lib/active_record/fixtures.rb8
-rw-r--r--activerecord/test/fixtures/db_definitions/schema.rb6
-rw-r--r--activerecord/test/fixtures/matey.rb4
-rw-r--r--activerecord/test/fixtures/mateys.yml4
-rwxr-xr-xactiverecord/test/fixtures_test.rb9
5 files changed, 28 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 97fca6026b..fbd614c969 100755
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -574,8 +574,8 @@ class Fixtures < YAML::Omap
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 @@ class Fixtures < YAML::Omap
@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)
diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb
index 12dbcb8423..9b6b6987d2 100644
--- a/activerecord/test/fixtures/db_definitions/schema.rb
+++ b/activerecord/test/fixtures/db_definitions/schema.rb
@@ -335,4 +335,10 @@ ActiveRecord::Schema.define do
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
diff --git a/activerecord/test/fixtures/matey.rb b/activerecord/test/fixtures/matey.rb
new file mode 100644
index 0000000000..86442e0470
--- /dev/null
+++ b/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
diff --git a/activerecord/test/fixtures/mateys.yml b/activerecord/test/fixtures/mateys.yml
new file mode 100644
index 0000000000..9ecdd4ecd5
--- /dev/null
+++ b/activerecord/test/fixtures/mateys.yml
@@ -0,0 +1,4 @@
+blackbeard_to_redbeard:
+ pirate_id: <%= Fixtures.identify(:blackbeard) %>
+ target_id: <%= Fixtures.identify(:redbeard) %>
+ weight: 10
diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb
index 6f11a337b1..de0321fbf4 100755
--- a/activerecord/test/fixtures_test.rb
+++ b/activerecord/test/fixtures_test.rb
@@ -10,6 +10,7 @@ require 'fixtures/category'
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 @@ class FasterFixturesTest < Test::Unit::TestCase
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 @@ class FoxyFixturesTest < Test::Unit::TestCase
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