Reputation: 1763
I used a migration in ruby to create these tables together with a model.
I have a Table Information and a Table Detail, each entry in the information table can have one association in the detail table.
So i created them with:
create_table :details do |t|
t.integer :id
t.string :Bezeichnung
t.binary :Koordinaten
end
create_table :informations do |t|
t.integer :id
t.integer :DetailID
t.integer :Longitude
t.integer :Latitude
end
In my Information Table i got a DetailID which should Reference to the id of the Detail table.
Now i did:
ALTER TABLE informations
ADD CONSTRAINT fk_informations_details
FOREIGN KEY (DetailID)
REFERENCES details(id)
Is this correct? Have i set the FOREIGN KEY correct? Or do i have to put the foreign key in the other table!?
Because i want to use in my Information Model following:
has_one :detail, :foreign_key => 'DetailID'
And in the Detail Model following:
belongs_to :main
Upvotes: 0
Views: 122
Reputation: 4555
Some notes:
detail_id
instead of DetailID
If Information has_one :detail
, Ruby on Rails will look for an entity with a matching information_id
in the details
table.
In your model, it is the other way around - the Information
contains a detail_id
. In other words Ruby on Rails would specify this as Information belongs_to :detail
and Detail has_one :information
.
Upvotes: 2