提交 f4b5ca1f 编写于 作者: D David Heinemeier Hansson

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

Fixed that create table with :id => false and fixtures don't play nice together (closes #10154) [jbarnette]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8205 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 ea713b6b
......@@ -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)
......
......@@ -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
class Matey < ActiveRecord::Base
belongs_to :pirate
belongs_to :target, :class_name => 'Pirate', :foreign_key => 'target_id'
end
blackbeard_to_redbeard:
pirate_id: <%= Fixtures.identify(:blackbeard) %>
target_id: <%= Fixtures.identify(:redbeard) %>
weight: 10
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册