首页 > 解决方案 > SQLSTATE [HY000]:一般错误:外键定义中有 1 个未知列“user_id”

问题描述

运行时出现此错误:

php artisan migrate:fresh

Illuminate\Database\QueryException:SQLSTATE[HY000]:一般错误:外键定义中有 1 个未知列“user_id”(SQL:创建表“users”(“id”整数不为空主键自动增量,“name”varchar 不为空, “email” varchar 不为 null,“username” varchar 不为 null,“email_verified_at” datetime null,“password” varchar 不为 null,“remember_token” varchar null,“created_at” datetime null,“updated_at” datetime null,外键(“user_id ") 在删除级联时引用 "users"("id")))

我正在关注 youtube 上的视频教程,教程的代码是这样的:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->string('url')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();

            $table->index('user_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles');
    }
}

如果我复制并粘贴此代码,则会出现错误。所以我在stackoverflow上搜索,我找到了这个解决方案:

public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->string('url')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();

            $table->index('user_id');             
        });

        Schema::table('profiles', function (Blueprint $table){
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('profiles');
    }

这是我的用户表:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('username')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('users');
    }

但是今天,当我跑步时,php artisan migrate:fresh我又遇到了这个错误。

我该如何解决?

谢谢

标签: sqldatabaselaravellaravel-artisan

解决方案


正如其他人所提到的,user_id不是表中的列users,但您正在尝试在其上创建索引。这一行:

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

属于profiles表创建模式,而不是users表创建模式。

完整代码:

// create_users_table.php
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('username')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

// create_profiles_table.php   <-- migrate AFTER users table
public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('title')->nullable();
        $table->text('description')->nullable();
        $table->string('url')->nullable();
        $table->string('image')->nullable();
        $table->timestamps();

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

推荐阅读