Laravel:违反完整性约束:1452 无法添加或更新子行:外键约束失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28934206/
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
Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
提问by Virgil Cruz
im having this error on my Laravel Application:
我的 Laravel 应用程序出现此错误:
Cannot add or update a child row: a foreign key constraint fails
无法添加或更新子行:外键约束失败
Here are my 2 tables
这是我的 2 张桌子
Users
用户
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('firstname');
$table->string('lastname');
$table->string('middlename');
$table->string('address');
$table->string('birthday');
$table->string('contact');
$table->string('email');
$table->enum('isAdmin', array(0,1))->default(0);
$table->enum('isTeacher', array(0,1))->default(0);
$table->timestamps();
});
Attendance
出勤率
Schema::create('attendance', function(Blueprint $table)
{
$table->increments('id');
$table->string('status');
$table->string('comment');
$table->integer('student_id')->unsigned();
$table->foreign('student_id')->references('id')->on('users');
$table->string('student_firstname');
$table->string('student_lastname');
$table->integer('teacher_id')->unsigned();
$table->foreign('teacher_id')->references('id')->on('users');
$table->string('teacher_firstname');
$table->string('teacher_lastname');
$table->timestamps();
Here's my models
这是我的模型
User
用户
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function attendance()
{
return $this->belongsTo('Attendance');
}
Attendance
出勤率
class Attendance extends Eloquent
{
public function users()
{
return $this->hasMany('User');
}
}
**And finally here's my controller **
**最后这是我的控制器**
public function postCreateAttendance()
{
$validate = Validator::make(Input::all(), array(
'status' => 'required'
));
if ($validate->fails())
{
return Redirect::route('viewStudent')->withErrors($validate)->withInput();
}
else
{
//$student = User::whereRaw('isTeacher = "0" and isAdmin = "0"')->get();
//$teacher = User::whereRaw('isTeacher = "1" and isAdmin = "0"')->get();
$student = new User();
$teacher = new User();
$attendance = new Attendance();
$student->save();
$teacher->save();
$attendance->save();
$student->student_id = Input::get('3');
$attendance->status = Input::get('status');
$attendance->comment = Input::get('comment');
$attendance->student_firstname = Input::get('student_first');
$attendance->student_lastname = Input::get('student_last');
$attendance->teacher_firstname = Input::get('teacher_first');
$attendance->teacher_lastname = Input::get('teacher_last');
I tried this too as well and its giving me the same error.
我也试过这个,它给了我同样的错误。
public function sampleAttendance()
{
User::find(1)->attendance()->insert(array(
'status' => 'present',
'comment' => 'none',
'student_id' => '1',
'student_firstname' => 'student_first',
'student_lastname' => 'student_last',
'teacher_firstname' => 'teacher_first',
'teacher_lastname' => 'teacher_last'
));
Please help? :(
请帮忙?:(
回答by Mysteryos
In your schema, you have placed integrity constraints, aka foreign keys in your attendance
table.
在您的架构中,您已经在attendance
表中放置了完整性约束,也就是外键。
Therefore you cannotsave an attendance record without adding its student_id
and teacher_id
first.
因此,您不能在不先添加student_id
和的情况下保存考勤记录teacher_id
。
This will not work:
这将不起作用:
$student = new User();
$teacher = new User();
$attendance = new Attendance();
$student->save();
$teacher->save();
$attendance->save();
This will work:
这将起作用:
$student = new User();
$teacher = new User();
//Saves student model
$student->save();
$teacher->save();
//Saves Attendance model in relationship with student and teacher model
$attendance = new Attendance([
'student_id' => $student->id,
'teacher_id' => $teacher->id
]);
$attendance->save();
Why can't i just associate the attendance model with the student and teacher models and then save them to the DB?
为什么我不能将考勤模型与学生和教师模型相关联,然后将它们保存到数据库中?
See Laravel Framework Issue: https://github.com/laravel/framework/issues/6437
请参阅 Laravel 框架问题:https: //github.com/laravel/framework/issues/6437
It's been proposed but not yet implemented.
已提出但尚未实施。