gpt4 book ai didi

php - 违反完整性约束 : 1452 laravel

转载 作者:可可西里 更新时间:2023-11-01 07:05:16 26 4
gpt4 key购买 nike

我在 laravel 中使用以下迁移创建了两个表:

用户迁移:

public function up()
{
Schema::create('users', function($table){
$table->increments('id')->unsigned();
$table->string('email')->unique();
$table->string('password', 64);
$table->string('first_name', 32);
$table->string('last_name', 32);
$table->string('remember_token', 100)->nullable();
});
}

场合迁移:

public function up()
{
Schema::create('occasions', function($table){
$table->increments('id')->unsigned();
$table->integer('created_by_user_id')->unsigned();
$table->foreign('created_by_user_id')->references('id')->on('users');
$table->integer('updated_by_user_id')->unsigned();
$table->foreign('updated_by_user_id')->references('id')->on('users');
$table->timestamps();
$table->string('title')->unique();
$table->string('slug')->unique();
$table->integer('category')->unsigned();
$table->foreign('category')->references('id')->on('occasion_categories');
$table->string('brand', 32);
$table->string('model', 32)->nullable();
$table->string('type', 32)->nullable();
$table->string('body', 32)->nullable();
$table->string('color', 32);
$table->integer('fuel')->unsigned()->nullable();
$table->foreign('fuel')->references('id')->on('occasion_fuels');
$table->integer('transmission')->unsigned()->nullable();
$table->foreign('transmission')->references('id')->on('occasion_transmissions');
$table->decimal('usage', 6, 2)->nullable();
$table->integer('engine_capacity')->unsigned()->nullable();
$table->integer('building_year')->unsigned()->nullable();
$table->string('sign', 8)->nullable();
$table->date('mot')->nullable();
$table->integer('kilometers')->nullable();
$table->decimal('price', 8, 2);
$table->decimal('action_price', 8, 2)->nullable();
$table->text('description')->nullable();
});
}

现在,当我尝试使用以下代码创建一个场合时:

Occasion::create(array(
'created_by_user_id'=>Auth::id(),
'updated_by_user_id'=>Auth::id(),
'title'=>Input::get('title'),
'slug'=>Str::slug(Input::get('title')),
'category'=>Input::get('category'),
'brand'=>Input::get('brand'),
'model'=>Input::get('model'),
'type'=>Input::get('type'),
'body'=>Input::get('body'),
'color'=>Input::get('color'),
'fuel'=>Input::get('fuel'),
'transmission'=>Input::get('transmission'),
'usage'=>Input::get('usage'),
'engine_capacity'=>Input::get('engine-capacity'),
'building_year'=>Input::get('building-year'),
'sign'=>Input::get('sign'),
'mot'=>Input::get('mot'),
'kilometers'=>Input::get('kilometers'),
'price'=>Input::get('price'),
'action_price'=>Input::get('action-price'),
'description'=>Input::get('description')
));

我收到以下错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (henw.occasions, CONSTRAINT occasions_created_by_user_id_foreign FOREIGN KEY (created_by_user_id) REFERENCES users (id)) (SQL: insert into occasions (updated_at, created_at) values (2014-06-30 18:42:11, 2014-06-30 18:42:11))

我在 Google 上搜索过,但大多数人说我收到此错误是因为外键不存在,但事实并非如此,因为当我尝试在 PhpMyAdmin 中添加相同的值时,它确实有效并且用户 ID我插入的是 1,它确实存在。

最佳答案

您最终的 sql 查询是 insert into occasions (updated_at, created_at) values (2014-06-30 18:42:11, 2014-06-30 18:42:11)。您应该看到只有 updated_atcreated_at 被插入。

发生这种情况是因为 Laravel 保护你的代码免受 Mass Assignment 的攻击默认情况下:

If user input is blindly passed into a model, the user is free to modify any and all of the model's attributes. For this reason, all Eloquent models protect against mass-assignment by default.

您需要通过在您的模型中添加一组 $fillable 列来使您的列可填充:

class Occasion extends Eloquent
{
protected $fillable = array(
'created_by_user_id',
'updated_by_user_id',
// The rest of the column names that you want it to be mass-assignable.
);
}

或者反其道而行之,保护任何你不希望它们被批量分配的列:

class Occasion extends Eloquent
{
protected $guarded = array(
// Any columns you don't want to be mass-assignable.
// Or just empty array if all is mass-assignable.
);
}

请注意,您正在将 Input::get() 直接分配给模型。这是来自 Mass Assignment 的警告部分:

Note: When using guarded, you should still never pass Input::get() or any raw array of user controlled input into a save or update method, as any column that is not guarded may be updated.

关于php - 违反完整性约束 : 1452 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24496935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com