# Referential integrity

**URL:** <https://www.ruby-forum.com/t/referential-integrity/165442>\
**Category:** Rails\
**Created:** [April 14, 2009, 12:54am UTC](https://www.ruby-forum.com/t/referential-integrity/165442 "2009-04-14T00:54:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doughie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@doughie](https://www.ruby-forum.com/u/doughie)\
**Post date:** [April 14, 2009, 12:54am UTC](https://www.ruby-forum.com/t/referential-integrity/165442/1 "2009-04-14T00:54:01Z")

</div>

I am starting out in rails and am loving it! Thanks to all the  
contributors!

I made a web app that had about 7 models, with some referring to each  
other, eg a team had a member and so on.

This relationship was not enforced in the model or in the database, it  
was kind of hacked together in the view by creating html select  
inputs.

I found very quickly that deleting models and adding new ones meant  
that objects that should not be related were.

I thought at first I should implement the correct cascading deletes.  
I have looked into this a little more and have now redefined all my  
models correctly, eg a team has\_many :members.

I looked at the mysql database this created and there are no foreign  
key relationships.

question 1) should there be foreign key relations in mysql db? I used  
the script/generate model and then rake db:migrate

my model:  
class Developer \< ActiveRecord::Base  
belongs\_to :team  
end

question 2) in my conceptual model my developer belongs to a team. So  
i have the model above. Should there be a relationship in team  
describing how the link exists to developer? Currently mine is empty:  
class Team \< ActiveRecord::Base  
end

thanks in advanced for any help!

---

<div class="post-metadata">

**Author:** ![doughie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@doughie](https://www.ruby-forum.com/u/doughie)\
**Post date:** [April 14, 2009, 1:44am UTC](https://www.ruby-forum.com/t/referential-integrity/165442/2 "2009-04-14T01:44:19Z")

</div>

On Apr 13, 11:06 pm, doughie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> question 1) should there be foreign key relations in mysql db? I used  
> the script/generate model and then rake db:migrate

Rails has no native support for foreign key constraints in the DB. I  
use the RedHillOnRails Core plugin, unfortunately there main site went  
offline not long ago but you can get the plugin from:  
[http://github.com/harukizaemon/redhillonrails\_core/tree/master](http://github.com/harukizaemon/redhillonrails_core/tree/master). This  
allows you to specify foreign keys in migrations if you want the  
database to maintain referential integrity for you (dunno who  
wouldn’t!).

> question 2) in my conceptual model my developer belongs to a team. So  
> i have the model above. Should there be a relationship in team  
> describing how the link exists to developer? Currently mine is empty:  
> class Team \< ActiveRecord::Base  
> end

You would say has\_many :developers. The Rails API  
ActiveRecord::Associations documentation provides an in-depth  
discussion of relations.

Best regards,  
Andrew

---

<div class="post-metadata">

**Author:** ![doughie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@doughie](https://www.ruby-forum.com/u/doughie)\
**Post date:** [April 14, 2009, 3:14am UTC](https://www.ruby-forum.com/t/referential-integrity/165442/3 "2009-04-14T03:14:54Z")

</div>

Your Team has many members and should be noted:

class Team \< ActiveRecord::Base  
has\_many :members  
end

This is assuming that you have a column in your member table named  
team\_id. I think this is what you were asking about with the foreign  
keys.

As a somewhat noob to Rails myself I found a few good books for  
beginners to be the most help. The two I like the best are: “Simply  
Rails 2” and “Agile Web D. with Rails”.
