Forum: Ruby on Rails has_and_belongs_to won't delete records

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2d0d904abb31102b049ad60e47c7af8e?d=identicon&s=25 Bart Gajderowicz (bart)
on 2007-01-25 04:06
(Received via mailing list)
Hi,
I have a has_and_belongs_to relationship between 2 objects, order and
product.  I'm getting a foreign key violation when trying to delete
Order.  Is there a special way of deleting orders without deleting
products? ("has_many" has :dependant => :delete_all.... does
"has_and_belongs_to" have something similar)

Trying:
Order.delete_all

Getting:
Mysql::Error: #23000Cannot delete or update a parent row: a foreign key
constraint fails: DELETE FROM orders


With associations:
class Order < ActiveRecord::Base
  has_and_belongs_to_many :products
end

class Product < ActiveRecord::Base
  has_and_belongs_to_many :orders
end


Thanks, Bart
2d0d904abb31102b049ad60e47c7af8e?d=identicon&s=25 Bart Gajderowicz (bart)
on 2007-01-25 07:18
(Received via mailing list)
Solved.. automatic deletion just doesn't work with
has_and_belongs_to_many.
I need to use has_many :through, :dependent => delete_all

Bart
This topic is locked and can not be replied to.