小编Muh*_*req的帖子

Laravel Migration外键约束不正确

迁移我的数据库时出现此错误,下面是我的代码,后面是我在尝试运行迁移时遇到的错误.

 public function up()
    {
        Schema::create('meals', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('category_id')->unsigned();
            $table->string('title');
            $table->string('body');
            $table->string('meal_av');
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('category_id')
                ->references('id')
                ->on('categories')
                ->onDelete('cascade');
        });
    }  
Run Code Online (Sandbox Code Playgroud)

错误信息

[Illuminate\Database\QueryException]                                         
      SQLSTATE[HY000]: General error: 1005 Can't create table `meal`.`#sql-11d2_1  
      4` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter   
      table `meals` add constraint meals_category_id_foreign foreign key (`catego  
      ry_id`) references `categories` (`id`) on delete cascade) 
Run Code Online (Sandbox Code Playgroud)

mariadb laravel-5 laravel-6

26
推荐指数
12
解决办法
4万
查看次数

标签 统计

laravel-5 ×1

laravel-6 ×1

mariadb ×1