This project is archived and is in readonly mode.

#2524 ✓invalid
Steven Parkes

duplicate SQL insert on has_many through

Reported by Steven Parkes | April 19th, 2009 @ 08:14 PM | in 2.x

I'm getting a strange case where saving an object that has a has_many/through does a double-SQL-insert on the join table. Unforunately, I can't get it to happen with a simple example (yet).

The left side of the relationshp is new. The right side is either new or not; doesn't seem to matter. The join table has a unique constraint, so an SQL error is thrown.

It runs the validations, the saves, and the after saves ... then it seems to run something again. This doesn't happen in the simple case, so in that case, it works. It looks from the stack trace like it's trying to run update on something (?) and thinks something is new still, a la @new_record_before_save in autosave_association.

Here's the stacktrace against 2-3-stable, but it happens against master and 2-2-stable as well.


.../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log': Mysql::Error: Duplicate entry '1230-40' for key 2: INSERT INTO `taggings` (`created_at`, `video_id`, `tag_id`, `updated_at`) VALUES('2009-04-19 18:53:19', 1230, 40, '2009-04-19 18:53:19') (ActiveRecord::StatementInvalid)
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:320:in `execute'
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:259:in `insert_sql'
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:330:in `insert_sql'
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:44:in `insert_without_query_dirty'
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:18:in `insert'
	from .../vendor/rails/activerecord/lib/active_record/base.rb:2902:in `create_without_timestamps'
	from .../vendor/rails/activerecord/lib/active_record/timestamp.rb:53:in `create_without_callbacks'
	from .../vendor/rails/activerecord/lib/active_record/callbacks.rb:266:in `create'
	from .../vendor/rails/activerecord/lib/active_record/base.rb:2868:in `create_or_update_without_callbacks'
	from .../vendor/rails/activerecord/lib/active_record/callbacks.rb:250:in `create_or_update'
	from .../vendor/rails/activerecord/lib/active_record/base.rb:2556:in `save_without_validation!'
	from .../vendor/rails/activerecord/lib/active_record/validations.rb:1019:in `save_without_dirty!'
	from .../vendor/rails/activerecord/lib/active_record/dirty.rb:87:in `save_without_transactions!'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:200:in `save!'
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:182:in `transaction'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:200:in `save!'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:200:in `save!'
	from .../vendor/rails/activerecord/lib/active_record/validations.rb:990:in `create!'
	from .../vendor/rails/activerecord/lib/active_record/reflection.rb:179:in `create_association!'
	from .../vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:60:in `insert_record'
	from .../vendor/rails/activerecord/lib/active_record/base.rb:2148:in `with_scope'
	from .../vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:60:in `send'
	from .../vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:60:in `insert_record'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:145:in `send'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:145:in `send'
	from .../vendor/rails/activerecord/lib/active_record/autosave_association.rb:294:in `save_collection_association'
	from .../vendor/plugins/will_paginate/lib/will_paginate/finder.rb:168:in `method_missing'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:368:in `method_missing_without_paginate'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:219:in `method_missing'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:219:in `each'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:219:in `send'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:219:in `method_missing'
	from .../vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:368:in `method_missing_without_paginate'
	from .../vendor/plugins/will_paginate/lib/will_paginate/finder.rb:168:in `method_missing'
	from .../vendor/rails/activerecord/lib/active_record/autosave_association.rb:286:in `save_collection_association'
	from .../vendor/rails/activerecord/lib/active_record/autosave_association.rb:169:in `autosave_associated_records_for_tags'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:181:in `evaluate_method'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:181:in `evaluate_method'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:181:in `instance_eval'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:181:in `evaluate_method'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in `call'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run'
	from .../vendor/rails/activesupport/lib/active_support/callbacks.rb:277:in `run_callbacks'
	from .../vendor/rails/activerecord/lib/active_record/callbacks.rb:344:in `callback'
	from .../vendor/rails/activerecord/lib/active_record/callbacks.rb:283:in `update'
	from .../vendor/rails/activerecord/lib/active_record/base.rb:2868:in `create_or_update_without_callbacks'
	from .../vendor/rails/activerecord/lib/active_record/callbacks.rb:250:in `create_or_update'
	from .../vendor/rails/activerecord/lib/active_record/base.rb:2539:in `save_without_validation'
	from .../vendor/rails/activerecord/lib/active_record/validations.rb:1009:in `save_without_dirty'
	from .../vendor/rails/activerecord/lib/active_record/dirty.rb:79:in `save_without_transactions'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:229:in `send'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
	from .../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:182:in `transaction'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:196:in `save'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
	from .../vendor/rails/activerecord/lib/active_record/transactions.rb:196:in `save'
	from ../../../../bin/../lib/.../import/map.rb:128:in `import'
	from .../vendor/gems/fastercsv-1.4.0/lib/faster_csv.rb:1514:in `each'
	from ../../../../bin/../lib/.../import/map.rb:38:in `import'
	from ../../../../bin/../lib/.../import.rb:52:in `import'
	from ../../../../bin/import:15

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages