Forum: Ruby on Rails foreign keys and migrations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c0a2cbea5455db466aed567de71bfd2?d=identicon&s=25 Andrew Gibson (esquilax)
on 2006-04-02 04:02
Hi:

I'm looking to get a point clarified which i'm sure is obvious to
everyone else  but I haven't been able to find the answer to.  i've
tried searching through the forums and the api pages and haven't found
anything.

Is there a way with migrations to set up foregin key relationships?
(beyond putting in the relevant foregin_id attributes)

Is that just supposed to be handeled at the model level with
"belongs_to" and "has_many" .etc... ?

I guess what I'm asking is whether relationships should just be handeled
at the business level or at the business level and data level? Is there
a best practices issue at work here?

lots of questions I know but I'm trying hard to learn:)


thanks!

Andrew
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-02 06:05
(Received via mailing list)
On Sun, 2006-04-02 at 04:02 +0200, Andrew Gibson wrote:
> Is that just supposed to be handeled at the model level with
> "belongs_to" and "has_many" .etc... ?
>
> I guess what I'm asking is whether relationships should just be handeled
> at the business level or at the business level and data level? Is there
> a best practices issue at work here?
>
> lots of questions I know but I'm trying hard to learn:)
----
assuming that rails 1.1 didn't change these things (and I didn't notice
an indication that it did change), I have them added at the bottom of
the appropriate migration file in raw sql form...

just before the 'end' tag for def self.up...

    # set up foreign key restratints for PostgreSQL
    execute 'ALTER TABLE ONLY clients ADD CONSTRAINT fk_cp_case_manager
FOREIGN KEY (case_manager_id) REFERENCES case_managers(id);'

etc. (note that my mail client will cause this to wrap on 2 lines but I
don't know that is significant).

Craig
0454b2d73007699b409d4d7a2bcf0b5f?d=identicon&s=25 oom tuinstoel (Guest)
on 2006-04-02 15:16
Craig White wrote:

>     execute 'ALTER TABLE ONLY clients ADD CONSTRAINT fk_cp_case_manager
> FOREIGN KEY (case_manager_id) REFERENCES case_managers(id);'
>

Do you need to set up foreign keys. I did not, and everything seems to
be working...
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-02 17:25
(Received via mailing list)
On Sun, 2006-04-02 at 15:16 +0200, oom tuinstoel wrote:
> Craig White wrote:
>
> >     execute 'ALTER TABLE ONLY clients ADD CONSTRAINT fk_cp_case_manager
> > FOREIGN KEY (case_manager_id) REFERENCES case_managers(id);'
> >
>
> Do you need to set up foreign keys. I did not, and everything seems to
> be working...
----
No - rails obtains the information from the models. This is for
PostgreSQL - i.e. if you are using the tables with other client
applications.

Craig
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-04-02 18:58
(Received via mailing list)
On a slight aside, one thing that would be nice is if you could
migrate the models along with the database. If I have to skip back to
a different version of my database for any reason, I find myself
having to comment out and change parts of my model to prevent it from
going all wrong.
-Nathan
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-02 19:10
(Received via mailing list)
subversion is your friend (version control)

svn revert

Craig
This topic is locked and can not be replied to.