【问题标题】:column exist but when migration it returns SQLSTATE[42000]: Syntax error or access violation: 1072 Key column doesn't exist in table列存在,但迁移时返回 SQLSTATE[42000]:语法错误或访问冲突:1072 表中不存在键列
【发布时间】:2020-04-01 16:51:46
【问题描述】:

列存在但迁移时返回

SQLSTATE[42000]:语法错误或访问冲突:1072 表中不存在键列

当我删除外键时,问题就解决了

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

   public function up()
{
    Schema::create('skills', function (Blueprint $table) {
        $table->increments('id');
        $table->string('skill_name')->nullable();
        $table->decimal('skill_note')->nullable();           
        $table->timestamps();
    });
}


   public function up()
    {
        Schema::create('skill_user', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->foreign('user_id ')
                ->references('id')->on('users');
            $table->unsignedInteger('skill_id');
            $table->foreign('skill_id')
                ->references('id')->on('skills');
            $table->decimal('note')->nullable();
            $table->timestamps();
        });
    }

【问题讨论】:

  • 显示您对 skillsusers 的迁移
  • 哪个版本的 Laravel?
  • @aynber 完成请帮忙:(
  • @kerbholz 版本 6
  • 如果您在上次迁移后对技能或用户表进行了任何修改,您必须运行命令php artisan migrate:fresh。因为在我看来,您提供的代码应该一切顺利。还要确保您的 skill_user 迁移在其他表之后运行。

标签: php laravel eloquent migration


【解决方案1】:

在第 6 版(您正在使用的那个我可以在 cmets 中读取的内容)中,increments('id') 方法不会创建无符号整数,而是创建无符号大整数,因此您需要将您的外键更改为

$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('skill_id');

【讨论】:

  • increments 创建 unsignedInteger 和 bigIncrements 创建 unsignedBigInteger 所以上面是对的。
【解决方案2】:

试试这个。

$table->integer('user_id')->unsigned();
$table->integer('skill_id')->unsigned();

如果它不起作用,我建议运行 composer install and migrate:fresh

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2015-10-12
    • 2023-04-01
    • 2018-05-31
    相关资源
    最近更新 更多