# Prevent deleting foreign key

**URL:** <https://www.ruby-forum.com/t/prevent-deleting-foreign-key/138468>\
**Category:** Rails\
**Created:** [May 23, 2008, 10:39am UTC](https://www.ruby-forum.com/t/prevent-deleting-foreign-key/138468 "2008-05-23T10:39:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [May 23, 2008, 10:39am UTC](https://www.ruby-forum.com/t/prevent-deleting-foreign-key/138468/1 "2008-05-23T10:39:09Z")

</div>

I’ve a Report and ReportType. The Report has one ReportType.  
Now I want prevent deleting of used ReportTypes. This is my ReportType  
class:

class ReportType \< ActiveRecord::Base  
validates\_presence\_of(:name);  
has\_many :reports  
end

And my Report class:

class Report \< ActiveRecord::Base  
belongs\_to :report\_type,  
:class\_name =\> “ReportType”,  
:foreign\_key =\> “report\_type\_id”  
end

I can create a query to find related reports, but I don’t think that’s  
the right way. Can anyone help?

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [May 23, 2008, 10:49am UTC](https://www.ruby-forum.com/t/prevent-deleting-foreign-key/138468/2 "2008-05-23T10:49:54Z")

</div>

> I can create a query to find related reports, but I don’t think that’s  
> the right way. Can anyone help?

That would be one option.  
Do that in an before\_destroy callback.

Option number two:  
use a counter\_cache.  
Add a column report\_count to ReportType  
and define

belongs\_to :report\_type,  
:class\_name =\> “ReportType”,  
:foreign\_key =\> “report\_type\_id”,  
:counter\_cache =\> true

Then, again in before\_destroy you can check for the number of records  
without an extra query (and comes in useful in many other situations)

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [May 23, 2008, 10:57am UTC](https://www.ruby-forum.com/t/prevent-deleting-foreign-key/138468/3 "2008-05-23T10:57:41Z")

</div>

> Then, again in before\_destroy you can check for the number of records  
> without an extra query (and comes in useful in many other situations)

But can I also place a restrict on delete in my database and catch that  
error?

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [May 23, 2008, 11:58am UTC](https://www.ruby-forum.com/t/prevent-deleting-foreign-key/138468/4 "2008-05-23T11:58:29Z")

</div>

On 23 May 2008, at 09:57, Sjoerd Schunselaar wrote:

> > Then, again in before\_destroy you can check for the number of records  
> > without an extra query (and comes in useful in many other situations)
> 
> But can I also place a restrict on delete in my database and catch  
> that  
> error?

Yes, create an actual foreign key constraint. rails won’t help you  
there, but you can execute arbitrary sql in your migrations (or  
anywhere else in fact), so just lookup foreign keys in the  
documentation of your database.

Fred
