# Strange Rails Behaviour with MySQL (delete cascade)

**URL:** <https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112>\
**Category:** Rails\
**Created:** [October 13, 2007, 3:14am UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112 "2007-10-13T03:14:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![havatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@havatar](https://www.ruby-forum.com/u/havatar)\
**Post date:** [October 13, 2007, 3:14am UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112/1 "2007-10-13T03:14:24Z")

</div>

hi,  
i have a problem that involve mysql. i habe two tables: groups,ranks  
ranks have a foraign key from groups with delete on cascade.  
In my ranks migration i use this

execute ‘ALTER TABLE ranks ADD FOREIGN KEY (group\_id) REFERENCES groups  
(id) ON DELETE CASCADE;’

if i do the following in the mysql console i get an empty result as  
expected.

DELETE FROM groups WHERE id=3; SELECT \* FROM ranks WHERE group\_id=3;

BUT here comes the Problem:

Group.find(3).destroy  
Rank.find\_by\_group\_id(3)

returns several ranks!!

First when i add “sleep 3” between these commands it works

Group.find(3).destroy  
sleep 3  
Rank.find\_by\_group\_id(3)

returns nil as expected  
but thats not the solution i really want…  
is it a rails bug or mysql problem?

Rank.find\_by\_group\_id(3) should first be executed if the  
delete-statement(with delete on cascade) is finished.  
is this shitty behaviour a rails bug or what? can some one help me out  
of this?

---

<div class="post-metadata">

**Author:** ![havatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@havatar](https://www.ruby-forum.com/u/havatar)\
**Post date:** [October 13, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112/2 "2007-10-13T13:29:59Z")

</div>

Use delete instead with exists? 🙂

if Group.exists?(3)  
Group.delete(3)  
end

Rank.find\_by\_group\_id(3)

---

<div class="post-metadata">

**Author:** ![havatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@havatar](https://www.ruby-forum.com/u/havatar)\
**Post date:** [October 13, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112/3 "2007-10-13T15:03:11Z")

</div>

I dont see any differences. can you explain me what it should do?

---

<div class="post-metadata">

**Author:** ![havatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@havatar](https://www.ruby-forum.com/u/havatar)\
**Post date:** [October 13, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112/4 "2007-10-13T15:22:40Z")

</div>

Andreas Kane wrote:

> I dont see any differences. can you explain me what it should do?

I had the same problem like you.

The destroy did not delete immediately, I had to wait few seconds before  
it did it.

The delete did what I wanted, and that was deleting it as soon as I call  
it, and then I could do whatever I want after that.

Did you try the code?

---

<div class="post-metadata">

**Author:** ![havatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@havatar](https://www.ruby-forum.com/u/havatar)\
**Post date:** [October 13, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112/5 "2007-10-13T19:31:35Z")

</div>

On 10/13/07, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it, and then I could do whatever I want after that.  
> You can also have AR handle the referential integrity:

class Group \< ActiveRecord::Base  
has\_many :ranks, :dependent =\> :destroy  
end

class Rank  
belongs\_to :group  
end

When you delete a group, AR will delete its ranks in the same  
transaction so you shouldn’t see the delay.

## Or course you might want to also declare the constraints in SQL if non AR code is manipulating the database.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![havatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@havatar](https://www.ruby-forum.com/u/havatar)\
**Post date:** [October 13, 2007, 7:43pm UTC](https://www.ruby-forum.com/t/strange-rails-behaviour-with-mysql-delete-cascade/117112/6 "2007-10-13T19:43:45Z")

</div>

the problem is that destroying of some childs is forbidden.  
that could be overrided by setting :dependent =\> destroy\_all

but there are much more models involved in this delete cascade stuff, so  
i had to define 5 has\_manys with :dependent=\> destroy\_all in the model  
on top an for them the finder sqls cause rails can not handle has manys  
through 2 models.
