aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-11-23 10:05:35 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2009-11-23 10:42:32 -0800
commit78790e47b8603917e2f2352f973a2de7769cb74b (patch)
tree12e6eb6a7c87c7ee85f9c961eefe8b685b69402c /activerecord/lib/active_record/associations.rb
parentd0aa0cfbd76a919e536e76d65419930fb7fe31da (diff)
downloadrails-78790e47b8603917e2f2352f973a2de7769cb74b.tar.gz
rails-78790e47b8603917e2f2352f973a2de7769cb74b.tar.bz2
rails-78790e47b8603917e2f2352f973a2de7769cb74b.zip
Revert "Revert "Assert primary key does not exist in habtm when the association is defined, instead of doing that everytime a record is inserted.""
This reverts commit 2b82708b0efb3a3458e8177beab58f0c585788ae. [#3128 state:resolved] Conflicts: activerecord/lib/active_record/associations.rb activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb10
1 files changed, 9 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index fc6f15206a..0fcd288fc5 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -61,6 +61,12 @@ module ActiveRecord
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.")
@@ -1675,7 +1681,6 @@ module ActiveRecord
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)
@@ -1685,6 +1690,9 @@ module ActiveRecord
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