【问题标题】:SQLSTATE[HY000]: General error: 1 unknown column "user_id" in foreign key definitionSQLSTATE [HY000]:一般错误:外键定义中有 1 个未知列“user_id”
【发布时间】:2019-10-24 18:38:46
【问题描述】:

我运行时出现此错误:

php artisan migrate:fresh

Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般 错误:外键定义中有 1 个未知列“user_id”(SQL: 创建表“用户”(“id”整数不为空主键自动增量, “名称” varchar 不为空,“电子邮件” varchar 不为空,“用户名” varchar not null, "email_verified_at" datetime null, "password" varchar not null, "remember_token" varchar null, "created_at" 日期时间 null, “updated_at”日期时间空,外键(“user_id”)引用 "users"("id") on delete cascade))

我在 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 时,我又遇到了这个错误。

我该如何解决?

谢谢

【问题讨论】:

  • 删除表并重新运行迁移!
  • 您的users 表迁移在哪里
  • 您应该检查您的迁移订单。 “users”表创建必须在“profiles”表创建之前运行。
  • @spyker 我正在使用用户表进行编辑

标签: sql database laravel laravel-artisan


【解决方案1】:

正如其他人所提到的,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');
    });
}

【讨论】:

    【解决方案2】:

    试试这个;

    public function up()
    {
        Schema::dropIfExists('profiles');
        Schema::create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->string('url')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();
    
            $table->index('user_id');             
        });
    }
    

    这就是迁移。在您能够运行它之前;删除迁移表中“CreateProfilesTable”的行。

    【讨论】:

      【解决方案3】:

      出现此问题的原因是配置文件表是在用户表之前创建的。 我有一个解决问题的窍门。

      您应该重命名迁移目录中的迁移文件。

      例如让我的文件名是这样的

      • 2019_06_10_000001_create_users_table.php
      • 2019_06_10_000002_create_profiles_table.php

      第一个用户表在您运行“artisan migrate or migrate:fresh”时创建

      为什么?因为第一个创建的迁移文件是用户文件。你应该仔细看看

      • 2019_06_10_000001 -> users 表名
      • 2019_06_10_000002 -> 配置文件表名称

      解决方案: 所以你只需要这样做:重命名“用户”表并将其命名为小于“配置文件”表的数字。这样问题就会解决。

      其他解决方案: 分别运行此命令后删除所有迁移文件。

      php artisan make:migration create_users_table
      php artisan make:migration create_profiles_table
      

      【讨论】:

        【解决方案4】:

        这里清楚地提到了错误:

        foreign key("user_id") references "users"("id") on delete cascade)
        

        并且您没有名称为 user_id 的列

        语法是:

        CONSTRAINT constraint_name
        FOREIGN KEY foreign_key_name (columns)  <-- this must be a column in the table
        REFERENCES parent_table(columns)
        ON DELETE action
        ON UPDATE action
        

        但在您的情况下,user_id 列在查询中不可用。因此,要么添加该列,要么删除此约束代码,然后重试。

        【讨论】:

          猜你喜欢
          • 2020-12-14
          • 2017-11-10
          • 2012-10-24
          • 2018-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-25
          相关资源
          最近更新 更多