The query you see in the error isn't the actual query. It's a simple approximation as pdo has no way of showing the actual query (as it uses bound parameters)
Maybe your editor has a more lax approach to strict mode or foreign keys
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I have a problem while inserting rows in a table:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (test_db.documents_rows, CONSTRAINT fk_dro_vat FOREIGN KEY (vat_type_id) REFERENCES vat_types (id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
insert into `documents_rows` (`document_id`, `product_id`, `product_name`, `vat_type_id`, `created_by`, `updated_at`, `created_at`)
values (4, 1, prodotto 1, 1, 1, 2022-07-18 18:33:24, 2022-07-18 18:33:24)
It's not a problem with the foreign key: if I add the missing delimiters, the query works fine:
insert into `documents_rows` (`document_id`, `product_id`, `product_name`, `vat_type_id`, `created_by`, `updated_at`, `created_at`)
values (4, 1, 'prodotto 1', 1, 1, '2022-07-18 18:33:24', '2022-07-18 18:33:24')
The Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id
* @property integer $document_id
* @property integer $product_id
* @property integer $vat_type_id
* @property string $product_name
* @property string $created_at
* @property string $updated_at
* @property integer $created_by
* @property Document $document
* @property VatType $vatType
* @property Product $product
*/
class DocumentsRow extends Model
{
/**
* @var array
*/
protected $fillable = ['document_id', 'product_id', 'vat_type_id', 'product_name', 'created_at', 'updated_at', 'created_by'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function document()
{
return $this->belongsTo('App\Models\Document');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function vatType()
{
return $this->belongsTo('App\Models\VatType');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product()
{
return $this->belongsTo('App\Models\Product');
}
}
The Migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDocumentsRowsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('documents_rows', function (Blueprint $table) {
$table->integer('id', true);
$table->integer('document_id')->default(0)->index('idx_document_id');
$table->integer('product_id')->default(0)->index('idx_product_id');
$table->string('product_name', 50);
$table->integer('vat_type_id')->default(0)->index('idx_vat_type_id');
$table->timestamp('created_at')->nullable()->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate()->nullable();
$table->integer('created_by')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('documents_rows');
}
}
I'm stuck. :-(
Please or to participate in this conversation.