Laravel5.5. migrate 提示外键约束错误?

n Connection.php line 664:

SQLSTATE[HY000]: General error: 1005 Can't create table n_fruit.#sql-3fa0_23 (errno: 150 "Foreign key constrain
t is incorrectly formed") (SQL: alter table posts add constraint posts_comment_id_foreign foreign key (comment _id) references comments (id) on delete cascade)

In Connection.php line 458:

SQLSTATE[HY000]: General error: 1005 Can't create table n_fruit.#sql-3fa0_23 (errno: 150 "Foreign key constrain
t is incorrectly formed")

Process finished with exit code 1 at 13:53:34.
Execution time: 12,000 ms.


$table->unsignedInteger('comment_id')->comment('评论id');
            $table->unsignedInteger('category_id')->comment('所属分类id');
            $table->foreign('comment_id')
                  ->references('id')
                  ->on('comments')
                  ->onDelete('cascade');

            $table->foreign('category_id')
                ->references('id')
                ->on('categorys')
                ->onDelete('cascade');

外键约束报错 求大神指导 感谢!

JellyBool

有没有先创建 comments 这个表?感觉你顺序反了。

hcweb 回复 JellyBool

群主 好 comments和categorys这两张表都有的 我再想是不是migrate创建的时候时间先后顺序有影响?

JellyBool 回复 hcweb

先后顺序的问题吧

hcweb 回复 JellyBool

我在数据库里面直接创建posts表的category_id和comment_id外键约束却可以 就是通过命令行不行!migrations里面创建的先后顺序是posts>categorys>comments!