违反完整性约束:1452 laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24496935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integrity constraint violation: 1452 laravel
提问by user3527894
I have two tables in laravel created with the following migrations:
我在 laravel 中有两个表,使用以下迁移创建:
User migration:
用户迁移:
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();
});
}
Occasion migration:
场合迁移:
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();
});
}
Now when I try to create a Occasion with the following code:
现在,当我尝试使用以下代码创建场合时:
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')
));
I get the following error:
我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
henw
.occasions
, CONSTRAINToccasions_created_by_user_id_foreign
FOREIGN KEY (created_by_user_id
) REFERENCESusers
(id
)) (SQL: insert intooccasions
(updated_at
,created_at
) values (2014-06-30 18:42:11, 2014-06-30 18:42:11))
SQLSTATE[23000]: 完整性约束违规:1452 无法添加或更新子行:外键约束失败 (
henw
.occasions
, CONSTRAINToccasions_created_by_user_id_foreign
FOREIGN KEY (created_by_user_id
) REFERENCESusers
(id
)) (SQL: insert intooccasions
(updated_at
,created_at
) values (2014-06-30) 18:42:11, 2014-06-30 18:42:11))
I searched on Google but most of the people say I get this error because the foreign key does not exists, but that isn't true, because when i try to add the same values in PhpMyAdmin it does work and the user id which i am inserting is 1 and it does exist.
我在 Google 上搜索过,但大多数人说我收到此错误是因为外键不存在,但事实并非如此,因为当我尝试在 PhpMyAdmin 中添加相同的值时,它确实有效,而且我的用户 ID插入是 1 并且它确实存在。
采纳答案by Unnawut
Your final sql query is insert into occasions (updated_at, created_at) values (2014-06-30 18:42:11, 2014-06-30 18:42:11)
. You should see that only updated_at
and created_at
is being inserted.
您的最终 sql 查询是insert into occasions (updated_at, created_at) values (2014-06-30 18:42:11, 2014-06-30 18:42:11)
. 您应该只看到updated_at
并且created_at
正在被插入。
This happens because Laravel protects your code against against Mass Assignmentby default:
发生这种情况是因为 Laravel 默认保护您的代码免受批量分配的影响:
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.
如果用户输入被盲目地传递到模型中,则用户可以自由修改模型的任何和所有属性。出于这个原因,所有 Eloquent 模型都默认防止大规模分配。
You'll need to make your columns fillable by adding an array of $fillable
columns in your model:
您需要通过$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.
);
}
Or do the opposite, guard any columns that you don't want them 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.
);
}
Note that you are assigning Input::get()
into the model directly. Here is a caution from Mass Assignmentsection:
请注意,您是Input::get()
直接分配到模型中。这是批量分配部分的警告:
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.
注意:使用受保护时,您仍然不应将 Input::get() 或任何用户控制输入的原始数组传递到保存或更新方法中,因为任何未受保护的列都可能被更新。