Skip to content

Commit

Permalink
Revert "Revert "Assert primary key does not exist in habtm when the a…
Browse files Browse the repository at this point in the history
…ssociation is defined, instead of doing that everytime a record is inserted.""

This reverts commit 2b82708.

[#3128 state:resolved]

Conflicts:

	activerecord/lib/active_record/associations.rb
	activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
  • Loading branch information
jeremy committed Nov 23, 2009
1 parent d0aa0cf commit 78790e4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
10 changes: 9 additions & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -61,6 +61,12 @@ def initialize(owner, reflection)
end
end

class HasAndBelongsToManyAssociationWithPrimaryKeyError < ActiveRecordError #:nodoc:
def initialize(reflection)
super("Primary key is not allowed in a has_and_belongs_to_many join table (#{reflection.options[:join_table]}).")
end
end

class HasAndBelongsToManyAssociationForeignKeyNeeded < ActiveRecordError #:nodoc:
def initialize(reflection)
super("Cannot create self referential has_and_belongs_to_many association on '#{reflection.class_name rescue nil}##{reflection.name rescue nil}'. :association_foreign_key cannot be the same as the :foreign_key.")
Expand Down Expand Up @@ -1675,7 +1681,6 @@ def create_belongs_to_reflection(association_id, options)

def create_has_and_belongs_to_many_reflection(association_id, options, &extension)
options.assert_valid_keys(valid_keys_for_has_and_belongs_to_many_association)

options[:extend] = create_extension_modules(association_id, extension, options[:extend])

reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self)
Expand All @@ -1685,6 +1690,9 @@ def create_has_and_belongs_to_many_reflection(association_id, options, &extensio
end

reflection.options[:join_table] ||= join_table_name(undecorated_table_name(self.to_s), undecorated_table_name(reflection.class_name))
if connection.supports_primary_key? && (connection.primary_key(reflection.options[:join_table]) rescue false)
raise HasAndBelongsToManyAssociationWithPrimaryKeyError.new(reflection)
end

reflection
end
Expand Down
@@ -1,11 +1,6 @@
module ActiveRecord
module Associations
class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc:
def initialize(owner, reflection)
super
@primary_key_list = {}
end

def create(attributes = {})
create_record(attributes) { |record| insert_record(record) }
end
Expand All @@ -23,9 +18,7 @@ def reset_column_information
end

def has_primary_key?
return @has_primary_key unless @has_primary_key.nil?
@has_primary_key = (@owner.connection.supports_primary_key? &&
@owner.connection.primary_key(@reflection.options[:join_table]))
@has_primary_key ||= @owner.connection.supports_primary_key? && @owner.connection.primary_key(@reflection.options[:join_table])
end

protected
Expand All @@ -40,11 +33,6 @@ def count_records
end

def insert_record(record, force = true, validate = true)
if has_primary_key?
raise ActiveRecord::ConfigurationError,
"Primary key is not allowed in a has_and_belongs_to_many join table (#{@reflection.options[:join_table]})."
end

if record.new_record?
if force
record.save!
Expand Down
16 changes: 2 additions & 14 deletions activerecord/test/cases/associations/habtm_join_table_test.rb
Expand Up @@ -36,21 +36,9 @@ def teardown
uses_transaction :test_should_raise_exception_when_join_table_has_a_primary_key
def test_should_raise_exception_when_join_table_has_a_primary_key
if ActiveRecord::Base.connection.supports_primary_key?
assert_raise ActiveRecord::ConfigurationError do
jaime = MyReader.create(:name=>"Jaime")
jaime.my_books << MyBook.create(:name=>'Great Expectations')
assert_raise ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError do
MyReader.has_and_belongs_to_many :my_books
end
end
end

uses_transaction :test_should_cache_result_of_primary_key_check
def test_should_cache_result_of_primary_key_check
if ActiveRecord::Base.connection.supports_primary_key?
ActiveRecord::Base.connection.stubs(:primary_key).with('my_books_my_readers').returns(false).once
weaz = MyReader.create(:name=>'Weaz')

weaz.my_books << MyBook.create(:name=>'Great Expectations')
weaz.my_books << MyBook.create(:name=>'Greater Expectations')
end
end
end
3 changes: 1 addition & 2 deletions activerecord/test/fixtures/edges.yml
@@ -1,6 +1,5 @@
<% (1..4).each do |id| %>
edge_<%= id %>:
id: <%= id %>
source_id: <%= id %>
sink_id: <%= id + 1 %>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion activerecord/test/schema/schema.rb
Expand Up @@ -160,7 +160,7 @@ def create_table(*args, &block)
t.integer :access_level, :default => 1
end

create_table :edges, :force => true do |t|
create_table :edges, :force => true, :id => false do |t|
t.column :source_id, :integer, :null => false
t.column :sink_id, :integer, :null => false
end
Expand Down

0 comments on commit 78790e4

Please sign in to comment.