首页 > 解决方案 > Laravel 迁移:“外键约束格式不正确”)

问题描述

当我尝试执行php artisan migratephp 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');

    });
}

标签: phpmysqllaravellaravel-migrations

解决方案


使用 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();
        });
    }


推荐阅读