Reputation: 1516
I am using two pivot tables to store two different relations of a table. One relation works fine on insert. but the next one throws the following error. I don't know what's causing this.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
sdesk
.approvers
, CONSTRAINTapprovers_user_id_foreign
FOREIGN KEY (user_id
) REFERENCESusers
(id
)) (SQL: insert intoapprovers
(user_id
,request_id
,updated_at
,created_at
) values (2, 6, 2014-04-29 10:54:37, 2014-04-29 10:54:37))
Here's my code:
UrequestsController.php
public function postCreate() {
$request = new Urequest;
$request->vms = Input::get('vms');
$request->location = Input::get('location');
$request->descr = Input::get('descr');
$request->status = Input::get('status');
//$request->role_group = Input::get('team');
$request->save();
$ruser = new Ruser;
$ruser->user_id = Input::get('userid');
$ruser->urequest()->associate($request);
$ruser->save();
$approver = new Approver;
$approver->user_id = Input::get('aid');
$approver->urequest()->associate($request);
$approver->save();
return Redirect::to('users/dashboard')->with('message', 'Saved!');
}
Approver.php
class Approver extends Eloquent {
protected $fillable = array('request_id', 'user_id');
public function urequest() {
return $this->belongsTo('Urequest','request_id');
}
}
Ruser.php
class Ruser extends Eloquent {
protected $fillable = array('request_id', 'user_id');
public function urequest() {
return $this->belongsTo('Urequest','request_id');
}
}
urequest.php
class Urequest extends Eloquent {
protected $table = 'requests';
protected $fillable = array('vms', 'location', 'descr', 'status');
public function ruser() {
return $this->hasOne('Ruser');
}
public function approver() {
return $this->hasOne('Approver');
}
}
Schema
Schema::create('requests', function($table)
{
$table->increments('id');
$table->string('vms', 20);
$table->string('location', 20);
$table->string('descr', 255);
$table->string('status', 20);
$table->timestamps();
});
Schema::create('rusers', function($table)
{
$table->increments('id');
$table->integer('request_id')->unsigned();
$table->foreign('request_id')->references('id')->on('requests');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Schema::create('approvers', function($table)
{
$table->increments('id');
$table->integer('request_id')->unsigned();
$table->foreign('request_id')->references('id')->on('requests');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Upvotes: 0
Views: 5611
Reputation: 87719
"A foreign key constraint" usually fails when you don't have a row for the value(s) you are inserting/updating in the foreign table.
In this case you probably doesn't have a user_id of 2 in the users table or a request_id of 6 in the requests table.
EDIT just saw morawcik answered it in comments.
Upvotes: 4