我試圖遷移我的Rails MySQL數據庫,我收到以下錯誤:
ActiveRecord :: StatementInvalid:Mysql2 :: Error:無法創建表development.comments(錯誤:150“外鍵約束形成錯誤”):CREATE TABLE注釋(id int AUTO_INCREMENT PRIMARY KEY,comment varchar(255),user_id int,post_id int,created_at datetime NOT NULL,updated_at datetime NOT NULL,INDEX index_comments_on_user_id(user_id),INDEX index_comments_on_post_id(post_id),CONSTRAINT fk_rails_03de2dc08c FOREIGN KEY(user_id)REFERENCES users(id),CONSTRAINT fk_rails_2fd19c0db7 FOREIGN KEY(post_id)REFERENCES posts( id))ENGINE = InnoDB
以下是我的遷移:
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :linkedin_username
t.string :facebook_username
t.string :facebook_id
t.string :profile_image
t.string :title_image
t.string :connection
t.boolean :team
t.boolean :active
t.boolean :admin
t.string :email
t.string :password_digest
t.timestamps
end
end
end
class CreateComments < ActiveRecord::Migration[5.0]
def change
create_table :comments do |t|
t.string :comment
t.references :user, foreign_key: true
t.references :post, foreign_key: true
t.timestamps
end
end
end
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :name
t.text :description
t.timestamps null: false
end
end
end
class CreatePosts < ActiveRecord::Migration[5.0]
def change
create_table :posts do |t|
t.string :title
t.string :description
t.references :category, foreign_key: true
t.references :user, foreign_key: true
end
end
end
如果有人能在這里給我一個想法,我將非常感激 . 我一直在谷歌搜索解決方案一段時間,但無法找到與Rails相關的任何內容 . 謝謝!