MySQL + Rails:errno:150“外键约束不正确地形成” [英] MySQL + Rails: errno: 150 "Foreign key constraint is incorrectly formed"
问题描述
我试图迁移我的Rails MySQL数据库,我得到以下错误:
I am trying to migrate my Rails MySQL database and I am getting the following error:
ActiveRecord :: StatementInvalid:Mysql2 ::错误:无法创建表
开发
。注释
(错误号:150外键约束未正确形成): CREATE TABLE注释
(id
int AUTO_INCREMENT PRIMARY KEY,注释
varchar(255),user_id
int,post_id
int,created_at
datetime NOT NULL,updated_at
datetime NOT NULL,INDEXindex_comments_on_user_id
(user_id
),INDEXindex_comments_on_post_id
(post_id
),CONSTRAINTfk_rails_03de2dc08c
FOREIGN KEY(user_id
)
REFERENCES用户
(id )
,CONSTRAINTfk_rails_2fd19c0db7
FOREIGN KEY(post_id
)
REFERENCESposts
(id
)
)ENGINE = InnoDB
ActiveRecord::StatementInvalid: Mysql2::Error: Can't create table
development
.comments
(errno: 150 "Foreign key constraint is incorrectly formed"): CREATE TABLEcomments
(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, INDEXindex_comments_on_user_id
(user_id
), INDEXindex_comments_on_post_id
(post_id
), CONSTRAINTfk_rails_03de2dc08c
FOREIGN KEY (user_id
) REFERENCESusers
(id
) , CONSTRAINTfk_rails_2fd19c0db7
FOREIGN KEY (post_id
) REFERENCESposts
(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。谢谢!
I would be very thankful if someone could give me an idea here. I have been googling for solutions for a while now but could not find anything on this related to Rails. Thank you!
推荐答案
我刚才找到了这个问题的答案。我的错误是相关的事实,我的帖子表迁移最后。我的注释表迁移引用了post表id父字段。一旦我重新生成注释表迁移并删除了以前的注释表迁移,一切都很好。
I just found the answer to this question. My error was related to the fact that my posts table migration came last. My comments table migration was referencing the post table id parent field. As soon as I regenerated the comment table migration and deleted the previous comment table migration, everything worked out fine.
这篇关于MySQL + Rails:errno:150“外键约束不正确地形成”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!