I had a data model generation script which works fine by invoking mysql command line utility. I want RoR to use this data model that includes several foreign key dependencies within the tables. I found that $ rake db:schema:dump creates the db/schema.rb file and it did, but it is missing all the foreign key constraints/clauses. This is obviously a problem.
I've watched the RoR tutorial on lynda.com and the foriegn key functionality is pretty basic and assumes a lot of conventions like there can only be one foreign key and it needs to have a naming convention of table1_table2_id or something.
I was chatting on IRC yesterday in the #rubyonrails channel and one person said that RoR ignores foriegn keys and the rake db:schema:dump seems to be doing so.
For example this is the mysql statement:
CREATE TABLE IF NOT EXISTS students
( studentid
INT NOT NULL AUTO_INCREMENT, teacherid
INT NOT NULL, parentid
INT NULL, firstname
VARCHAR(45) NOT NULL, lastname
VARCHAR(45) NOT NULL, dob
DATETIME NOT NULL, isadult
TINYINT(1) NOT NULL, email
VARCHAR(45) NOT NULL, cellphone
INT NULL, username
VARCHAR(45) NOT NULL, password
VARCHAR(45) NOT NULL, addr_streetno
VARCHAR(45) NULL, addr_aptno
INT NULL, addr_city
VARCHAR(45) NULL, addr_state
VARCHAR(45) NULL, addr_zip
INT NULL, photo
MEDIUMBLOB NULL, PRIMARY KEY ( studentid
), INDEX parentid_idx
( parentid
ASC), INDEX teacherid_idx
( teacherid
ASC), CONSTRAINT parentid
FOREIGN KEY ( parentid
) REFERENCES parents
( parentid
) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT teacherid
FOREIGN KEY ( teacherid
) REFERENCES teachers
( teacherid
) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB;
In the schema.rb, it shows up as this and doesn't have any directives for foreign keys. what gives:
create_table "students", primary_key: "studentid", force: true do |t|
t.integer "teacherid", null: false
t.integer "parentid"
t.string "firstname", limit: 45, null: false
t.string "lastname", limit: 45, null: false
t.datetime "dob", null: false
t.boolean "isadult", null: false
t.string "email", limit: 45, null: false
t.integer "cellphone"
t.string "username", limit: 45, null: false
t.string "password", limit: 45, null: false
t.string "addr_streetno", limit: 45
t.integer "addr_aptno"
t.string "addr_city", limit: 45
t.string "addr_state", limit: 45
t.integer "addr_zip"
t.binary "photo", limit: 16777215
end
add_index "students", ["parentid"], name: "parentid_idx", using: :btree
add_index "students", ["teacherid"], name: "teacherid_idx", using: :btree
in Rails, where to place the foreign key
is determined by the association of your models.
Let's say you have the following two models:
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
the foreign key will be placed in the table declaring the belongs_to
association. in this case, the children
table.
the foreign key is the name of the parent model
with the suffix _id
. in this case, it would be parent_id
.
In Rails, this is how you add parent_id
to the children_table
:
rails g migration AddParentIdToChild parent_id:integer
then run rake db:migrate
. afterwards, the foreign key should show up in your schema
I hope I didn't misunderstood your question.
For more info about foreign keys and tables, see the documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.