# Question about one-to-many-to-many

**URL:** <https://www.ruby-forum.com/t/question-about-one-to-many-to-many/59812>\
**Category:** Rails\
**Created:** [May 6, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/question-about-one-to-many-to-many/59812 "2006-05-06T23:38:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [May 6, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/question-about-one-to-many-to-many/59812/1 "2006-05-06T23:38:32Z")

</div>

Guys,

I’ve set up a 1-M-M relationship between 3 tables/models. I can’t get  
cascading deletes (haven’t tried updates yet) to work when the master  
record is deleted. I’m using MySQL 4.1.x, and the error message is:

Mysql::Error: #23000Cannot delete or update a parent row: a foreign key  
constraint fails: DELETE FROM contact\_addresses WHERE (contact\_id = 1).

Is this something that’s not supported by MySQL? Or might I have setup  
my models (has\_many, belongs\_to, etc.) incorrectly?

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [May 6, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/question-about-one-to-many-to-many/59812/2 "2006-05-06T23:52:10Z")

</div>

Oops, forgot to mention - the error above is referring to the 1st M  
table. What I believe is obviously happening is that the master record  
in the 1st M table is being deleted, but it can’t because there are  
child records still in the 2nd M table.

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [May 7, 2006, 6:03am UTC](https://www.ruby-forum.com/t/question-about-one-to-many-to-many/59812/3 "2006-05-07T06:03:09Z")

</div>

What do your relationships look like?

---

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [May 8, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/question-about-one-to-many-to-many/59812/4 "2006-05-08T14:44:53Z")

</div>

Adam,

Logically speaking, a contact can have multiple addresses, and each  
address can have multiple phone numbers:

Contact (master):

has\_many :contact\_addresses, :dependent =\> :delete\_all

ContactAddress (detail 1):

belongs\_to :contact  
has\_many :addresses\_phones, :dependent =\> :delete\_all  
has\_many :contact\_phones, :through =\> :addresses\_phones

ContactPhone (detail 2):

belongs\_to :contact\_addresses  
has\_many :addresses\_phones, :dependent =\> :delete\_all  
has\_many :contact\_addresses, :through =\> :addresses\_phones

The addresses\_phones table (which links addresses and phones) is not in  
the model, and simply contains contact\_address\_id and a contact\_phone\_id  
columns.

I’m wondering if I need to write a line or 2 of code that first deletes  
the phone records so that Rails can then delete the address and contact  
records.

Here’s the inspiration for all of the above:

[http://tinyurl.com/kk5gt](http://tinyurl.com/kk5gt)
