【发布时间】:2020-03-12 19:01:29
【问题描述】:
我发现发布了许多类似的问题,但似乎没有一个解决方案适用于我的情况。当我运行“php artisan migrate:fresh”时,它会抛出错误......
Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束(SQL:alter table
slicer_profiles添加约束slicer_profiles_user_id_foreign删除级联时外键 (user_id) 引用users(id)
- 创建的所有表都是 InnoDB
- “用户”表在我的表之前创建
-
我将代码分为两步,然后分配外键
Schema::create('slicer_profiles', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->string('title'); $table->text('description'); $table->string('slicer'); $table->string('machine'); $table->softDeletes(); $table->timestamps(); }); Schema::table('slicer_profiles', function($table) { $table->foreign('user_id')->unsigned() ->references('id') ->on('users') ->onDelete('cascade'); });
我检查了 auth users 表,它似乎使用了 UNSIGNED BIGINT,所以我也尝试在引用上设置 ->unsigned(),但没有改变。任何解决此问题的帮助将不胜感激!
【问题讨论】:
标签: php mysql laravel foreign-keys database-migration