SQLSTATE [23000]:外键

问题描述 投票:0回答:2

您好,我仍然收到此错误,但是一切看起来不错,并且迁移工作正常。

SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败([fakturytablefakturas,约束fakturas_id_sprzedawcy_foreign外键(id_sprzedawcy)在删除级联中的引用sprzedawcasid)(SQL:插入fakturastyp_fakturydata_wystawieniamejsce_wystawieniadata_sprzedazytowar_uslugajmilosccena_nettowatosc_nettostawka_vatkwota_vatwartosc_bruttostatussposob_platnoscinumer_kontatermin_platnosci)值(fsturaVAT,2020.10.14,明斯克Mazowiecki,2020.10.26,Porty,1月10日,23456,421124,1234,207,1107,nieaplacona,qwrq,12345678901234567890098321,2020.10.28)))>

fakturas迁移:

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

class CreateFakturasTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('fakturas', function(Blueprint $table){      
            $table->increments('id');
            $table->unsignedinteger('id_sprzedawcy');
            $table->unsignedinteger('id_nabywcy');
            $table->string('typ_faktury');
            $table->date('data_wystawienia');
            $table->string('mejsce_wystawienia');
            $table->date('data_sprzedazy');
            $table->string('towar_usluga');
            $table->string('jm');
            $table->bigInteger('ilosc');
            $table->bigInteger('cena_netto');
            $table->bigInteger('watosc_netto');
            $table->bigInteger('stawka_vat');
            $table->bigInteger('kwota_vat');
            $table->bigInteger('wartosc_brutto');
            $table->string('status');
            $table->string('sposob_platnosci');
            $table->string('numer_konta');
            $table->date('termin_platnosci');


        });
        Schema::table('fakturas',function($table){
            $table->foreign('id_sprzedawcy')
            ->references('id')->on('sprzedawcas')
            ->onDelete('cascade');
        $table->foreign('id_nabywcy')
            ->references('id')->on('nabywcas')
            ->onDelete('cascade');
        });

    }
    public function down()
    {

       Schema::dropIfExists('fakturas');
       Schema::enableForeignKeyConstraints();
    }
}

您好,我仍然收到此错误,但是一切看起来不错,并且迁移工作正常。 SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键...

php laravel laravel-5.8
2个回答
0
投票

您的插入不正确。


0
投票

在插入时,您没有提供id_sprzedawcy的值(该值应该是sprzedawcas表中存在的ID)。要么在create语句中提供它,要么在迁移中使其为空:

$table->unsignedinteger('id_sprzedawcy')->nullable();
© www.soinside.com 2019 - 2024. All rights reserved.