【问题标题】:Getting "Integrity constraint violation: 1452" in Laravel在 Laravel 中获取“完整性约束违规:1452”
【发布时间】:2014-06-15 07:55:57
【问题描述】:

我正在使用两个数据透视表来存储一个表的两个不同关系。一种关系在插入时工作正常。但下一个会引发以下错误。不知道是什么原因造成的。

SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(sdesk.approvers,CONSTRAINT approvers_user_id_foreign FOREIGN KEY(user_id)参考users (id)) (SQL: 插入approvers (user_id, request_id, updated_at, created_at) 值 (2, 6, 2014-04-29 10:54:37, 2014- 04-29 10:54:37))

这是我的代码:

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::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();
    });

【问题讨论】:

  • 您有 id 为“2”的用户吗?
  • 刚才看到了。我删除了 id 为 2 的用户。感谢将其发布到答案中
  • 我遇到了类似的问题。我已将“类型”列链接到查找表,并试图将其留空,这违反了外键约束。我几乎不记得我的架构中有第二个外键约束,所以我一直在查看我的“用户”外键并想知道哪里出了问题。我想为可能会撞到墙上的其他人发布另一种可能性。

标签: php mysql laravel laravel-4 eloquent


【解决方案1】:

当您在外部表中插入/更新的值没有一行时,“外键约束”通常会失败。

在这种情况下,您可能在 users 表中没有 2 的 user_id 或在 requests 表中没有 6 的 request_id。

EDIT 刚刚看到 morawcik 在 cmets 中回答了它。

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 2016-05-25
    • 2021-07-23
    • 2020-04-22
    • 1970-01-01
    • 2017-02-28
    • 2012-02-26
    • 2012-09-19
    相关资源
    最近更新 更多