【问题标题】:SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'proform_id' doesn't exist in tableSQLSTATE [42000]:语法错误或访问冲突:1072 表中不存在键列“proform_id”
【发布时间】:2020-12-07 17:39:55
【问题描述】:

在 php artisan migrate:fresh 后出现错误:

SQLSTATE[42000]:语法错误或访问冲突:1072 表中不存在键列“proform_id”(SQL:更改表proforms 添加约束proforms_proform_id_foreign 外键(proform_id)引用proforms (id) 删除级联)

这是产生错误的迁移: 2020_08_08_093303_create_dynamic_field.php

<?php

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

class CreateDynamicField extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dynamic_fields', function (Blueprint $table) {
            $table->increments('id');
            $table->string('id_pozycji')->nullable();
            $table->string('name')->nullable();
            $table->string('PKWIU')->nullable();
            $table->integer('quantity')->nullable();
            $table->integer('unit')->nullable();
            $table->integer('netunit')->nullable();
            $table->integer('nettotal')->nullable();
            $table->integer('VATrate')->nullable();
            $table->integer('grossunit')->nullable();
            $table->integer('grosstotal')->nullable();
            $table->integer('proform_id')->nullable();
            $table->timestamps();
            $table->time('deleted_at')->nullable();
        });

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





    }

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

这是与之相关的迁移: 2020_07_29_101958_proforms.php

<?php

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

class Proforms extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('proforms', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('proformnumber')->nullable();
            $table->date('proformdate')->nullable();
            $table->date('selldate')->nullable();
            $table->integer('user_id')->unsigned()->nullable();
            $table->integer('form_id')->unsigned()->nullable();                        
            $table->integer('currency_id')->unsigned()->nullable();
            $table->string('paymentmethod')->nullable();
            $table->date('paymentdate')->nullable();
            $table->string('status')->nullable();
            $table->string('comments')->nullable();
            $table->string('city')->nullable();            
            $table->string('autonumber')->nullable();
            $table->integer('automonth')->nullable();
            $table->integer('autoyear')->nullable();
            $table->string('name')->nullable();
            $table->string('PKWIU')->nullable();
            $table->integer('quantity')->nullable();
            $table->integer('unit')->nullable();
            $table->integer('netunit')->nullable();
            $table->integer('nettotal')->nullable();
            $table->integer('VATrate')->nullable();
            $table->integer('grossunit')->nullable();
            $table->integer('grosstotal')->nullable();
            $table->timestamps();
            $table->time('deleted_at')->nullable();
        });
        
        Schema::table('proforms', function (Blueprint $table){
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
            
            $table->foreign('form_id')
                  ->references('id')
                  ->on('forms')
                  ->onDelete('cascade');                
    

            $table->foreign('currency_id')
                  ->references('id')
                  ->on('currencys')
                  ->onDelete('cascade');
            
            
            
            
            
            
        });
    

【问题讨论】:

  • 您的proforms 表确实没有proform_id... 为什么要尝试将FK (proform_id) 设置为同一个表(proforms)?

标签: php mysql laravel web-frameworks laravel-migrations


【解决方案1】:

我觉得这很奇怪:

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

您似乎正在尝试添加一个引用它自己的表的外键。

proforms 表上没有 proform_id。此语句需要在dynamic_fields 表上运行。

Schema::table('dynamic_fields', function (Blueprint $table) {
    $table->foreign('proform_id')
          ->references('id')
          ->on('proforms')
          ->onDelete('cascade');            
});

【讨论】:

  • 更改后我还有其他错误:SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:更改表dynamic_fields添加约束dynamic_fields_proform_id_foreign外键(@987654328 @) 在删除级联时引用 proforms (id)
  • @Piotr,外键应该是无符号的,把$table-&gt;integer('proform_id')-&gt;nullable();改成$table-&gt;integer('proform_id')-&gt;unsigned()-&gt;nullable();
  • 确保两个表使用相同的数据库引擎(即 InnoDB )并且proforms.id 列和dynamic_fields.proform_id 都使用相同的数据类型(即两者都是integerbigInteger)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 2023-04-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多