[英]Integrity constraint violation: 1452 laravel
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
,约束occasions_created_by_user_id_foreign
外键(created_by_user_id
)参考users
(id
))(SQL:INSERT INTOoccasions
(updated_at
,created_at
)值(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,并且确实存在。
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)
. 您的最终SQL查询将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. 您应该看到仅插入了updated_at
和created_at
。
This happens because Laravel protects your code against against Mass Assignment by 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
列数组来使列可$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. 请注意,您是直接将Input::get()
分配给模型。 Here is a caution from Mass Assignment section: 这是“ 批量分配”部分的警告:
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()或用户控制输入的任何原始数组传递到保存或更新方法中,因为任何不受保护的列都可能会更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.