Dan Rubio
Dan Rubio

Reputation: 4907

Does rails delete method trigger a postgresql DELETE CASCADE?

I have a table called accounts that is extremely large ~600,000+. From this table, I need to delete about 1/3 of the accounts. On the accounts table are many references to different tables. What I want to do is to delete the accounts by batches to speed up the process but it looks like the Rails delete method does not do that.

So that is my question. Does the delete method trigger the DELETE CASCADE command? I tried it out on the rails console and it would seem that it in fact does not trigger a DELETE CASCADE. Is the only purpose of the delete method is to not trigger rails callbacks? Is there a different method that I can look into?

Upvotes: 0

Views: 185

Answers (1)

Paul A Jungwirth
Paul A Jungwirth

Reputation: 24551

There is not actually a Postgres command called DELETE CASCADE. But if you have foreign keys with the option ON DELETE CASCADE, then yes, when you delete a row, the rows that reference it will be deleted too. None of this is really Rails, but just Postgres.

Upvotes: 2

Related Questions