php - Laravel 迁移:“外键约束格式不正确”)
问题描述
当我尝试执行php artisan migrate
或php artisan migrate:refresh
出现此错误时,我的迁移失败:
errno: 150 "Foreign key constraint is incorrectly formed"
代码
用户表(迁移成功)
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->integer('id',)->primary();
$table->string('name', 255);
$table->string('surname', 255);
$table->string('email', 255)->unique();
$table->string('password', 255);
$table->string('profile_image', 255);
$table->string('profile_cover', 255);
$table->rememberToken();
$table->char('token', 255)->unique()->nullable()->default(null);
$table->enum('role', ['user', 'driver', ',merchant', 'storemanager', 'storeoperator', 'company', 'employee']);
$table->dateTime('created');
$table->dateTime('updated');
});
}
地址表(因错误而失败)
public function up()
{
Schema::create('address', function (Blueprint $table) {
$table->integer('id')->primary();
$table->string('address1');
$table->string('address2');
$table->string('city');
$table->integer('country')->unsigned();
$table->decimal('latitude');
$table->decimal('longitude');
$table->string('instructions');
$table->integer('default');
$table->integer('user')->unsigned();
$table->dateTime('created');
$table->dateTime('updated');
$table->foreign('country')->references('id')->on('country')->onUpdate('cascade');
$table->foreign('user')->references('id')->on('users')->onUpdate('cascade');
});
}
解决方案
使用 Laravel 等框架的原因之一是语法糖。通过遵循他们的约定,您编写的代码更少,并且必须考虑更少的应用程序的“具体细节”。
你正在做与此相反的事情,并且已经看到了后果。一旦你尝试建立模型及其关系,你会经历更多的痛苦。相反,您应该使用如下所示的迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAddressesTable extends Migration
{
public function up()
{
// table names are plural of the object name
Schema::create('addresses', function (Blueprint $table) {
// automatically create an incrementing bigint column called 'id'
$table->id();
// name foreign ID columns correctly to save yourself trouble later
$table->foreignId('country_id')->constrained();
$table->foreignId('user_id')->constrained();
// specify lengths for your varchar columns
$table->string('address1', 100);
$table->string('address2', 100);
$table->string('city', 100);
$table->decimal('latitude');
$table->decimal('longitude');
$table->string('instructions', 100);
$table->integer('default');
// why would you define your own timestamp columns with non-standard names?
$table->timestamps();
});
}
}
还有users
桌子;您希望类似地更新其迁移。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
// DO NOT use an enum column; create another table called roles instead
$table->foreignId('role_id')->constrained();
$table->string('name', 100);
$table->string('surname', 100);
$table->string('email', 100)->unique();
$table->string('password', 255);
$table->string('profile_image', 255);
$table->string('profile_cover', 255);
$table->rememberToken();
//
$table->char('token', 255)->unique()->nullable()->default(null);
$table->timestamps();
});
}
推荐阅读
- php - mongo db 使用 mongo php 在文档中添加/更新数组
- python - 如何过滤包含字典列表的字典
- r - 在第一个循环-R 处固定一个值
- spring - 如何在 Spring Data MongoRepository 中使用事务?
- python - Django/Python - 将文件从命令输出保存到 ImageField
- javascript - 由于 npm 未导航到不正确的目录,无法运行服务器。并找到 package.jason
- python - 将构建的字典类型转换为 TypedDict
- netsuite - 从 V1 用户事件调用 V2 模块
- c++ - 将字符串转换为类
- typescript - Jest 遇到了 NUXT 打字稿的意外标记