mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Removes the restriction on primary key when joining in a habtm && test that it was properly removed
This commit is contained in:
parent
bf9272b9d4
commit
ec44763f03
3 changed files with 4 additions and 15 deletions
|
@ -76,12 +76,6 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
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:
|
class HasAndBelongsToManyAssociationForeignKeyNeeded < ActiveRecordError #:nodoc:
|
||||||
def initialize(reflection)
|
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.")
|
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.")
|
||||||
|
|
|
@ -39,10 +39,6 @@ module ActiveRecord::Associations::Builder
|
||||||
model.send(:undecorated_table_name, model.to_s),
|
model.send(:undecorated_table_name, model.to_s),
|
||||||
model.send(:undecorated_table_name, reflection.class_name)
|
model.send(:undecorated_table_name, reflection.class_name)
|
||||||
)
|
)
|
||||||
|
|
||||||
if model.connection.supports_primary_key? && (model.connection.primary_key(reflection.options[:join_table]) rescue false)
|
|
||||||
raise ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError.new(reflection)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generates a join table name from two provided table names.
|
# Generates a join table name from two provided table names.
|
||||||
|
|
|
@ -33,12 +33,11 @@ class HabtmJoinTableTest < ActiveRecord::TestCase
|
||||||
ActiveRecord::Base.connection.drop_table :my_books_my_readers
|
ActiveRecord::Base.connection.drop_table :my_books_my_readers
|
||||||
end
|
end
|
||||||
|
|
||||||
uses_transaction :test_should_raise_exception_when_join_table_has_a_primary_key
|
uses_transaction :test_should_not_raise_exception_when_join_table_has_a_primary_key
|
||||||
def test_should_raise_exception_when_join_table_has_a_primary_key
|
def test_should_not_raise_exception_when_join_table_has_a_primary_key
|
||||||
if ActiveRecord::Base.connection.supports_primary_key?
|
if ActiveRecord::Base.connection.supports_primary_key?
|
||||||
assert_raise ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError do
|
# This test is to confirm that this feature is now gone
|
||||||
MyReader.has_and_belongs_to_many :my_books
|
assert MyReader.has_and_belongs_to_many(:my_books).is_a?(ActiveRecord::Reflection::AssociationReflection)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue