【发布时间】:2020-07-08 05:17:05
【问题描述】:
我想在我的 laravel 7.x 应用程序上使用两个模型:用户和图像:
# Users migration : 2014_10_12_000000_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
# Images migration : 2020_03_27_121254_create_models_images_table
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned;
$table->string('name');
$table->timestamps();
});
Schema::table('images', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
尝试迁移时,我收到以下错误:一般错误:1215 无法添加外键约束(SQL:更改表 images 添加约束 images_user_id_foreign 外键 (user_id) 引用 users (id)删除级联)
我已经在谷歌上搜索过,但没有成功,有人可以帮助我吗?
谢谢
【问题讨论】:
-
如果以上都不适合你,看看这个。 stackoverflow.com/a/71251473/8325024