在 Laravel 中获取“违反完整性约束:1452”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23363637/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:26:32  来源:igfitidea点击:

Getting "Integrity constraint violation: 1452" in Laravel

phpmysqllaravellaravel-4eloquent

提问by winnyboy5

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, CONSTRAINT approvers_user_id_foreignFOREIGN KEY (user_id) REFERENCES users(id)) (SQL: insert into approvers(user_id, request_id, updated_at, created_at) values (2, 6, 2014-04-29 10:54:37, 2014-04-29 10:54:37))

SQLSTATE[23000]: 完整性约束违规:1452 无法添加或更新子行:外键约束失败 ( sdesk. approvers, CONSTRAINT approvers_user_id_foreignFOREIGN KEY ( user_id) REFERENCES users( id)) (SQL: insert into approvers( 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

请求控制器.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

审批人.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();
    });

回答by Antonio Carlos Ribeiro

"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.

在这种情况下,用户表中的 user_id 可能不为 2,请求表中的 request_id 可能不为 6。

EDITjust saw morawcik answered it in comments.

编辑刚刚看到 morawcik 在评论中回答了它。