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

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

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

这是生成错误的迁移: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');

    }

}


浮云间
浏览 133回答 2
2回答

白猪掌柜的

这对我来说看起来很奇怪:Schema::table('proforms', function (Blueprint $table) {&nbsp; &nbsp; $table->foreign('proform_id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->references('id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->on('proforms')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->onDelete('cascade');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;});看起来您正在尝试添加一个外键并引用它自己的表。proform_id桌子上没有proforms。该语句需要在表上运行dynamic_fields。Schema::table('dynamic_fields', function (Blueprint $table) {&nbsp; &nbsp; $table->foreign('proform_id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->references('id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->on('proforms')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->onDelete('cascade');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;});

开满天机

尝试添加foreignId而不是像这样只添加foreignId:Schema::table('proforms', function (Blueprint $table){&nbsp; &nbsp; &nbsp; &nbsp; $table->foreignId('user_id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->references('id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->on('users')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->onDelete('cascade');
打开App,查看更多内容
随时随地看视频慕课网APP