SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败

时间:2015-04-22 13:08:53

标签: php mysql laravel-4

我知道这是一个常见问题但不知道这里有什么问题。正如您所看到的那样//return $user,它显示了一个有效的ID。在数据库中也检查过。

        $user = new User;
        $user->first_name           = $data['first_name'];
        $user->last_name            = $data['last_name'];
        $user->email                = $data['email'];
        $user->phone_no             = $data['phone_no'];
        $user->created_from         = 'Web App';
        $user->save();
        // return $user;
        Session::put('user_id',$user->id);
        // return $user->id;
        $address = new Address;
        $address->user_id           = $user->id;
        $address->first_name        = $data['receiver_first_name'];
        $address->last_name         = $data['receiver_last_name'];
        $address->email             = $data['receiver_email'];
        $address->address_line_1    = $data['receiver_address_line_1'];
        $address->address_line_2    = $data['receiver_address_line_2'];
        $address->landmark          = $data['receiver_landmark'];
        $address->pincode           = $data['receiver_pincode'];
        $address->phone_no          = $data['receiver_phone_no'];
        $address->created_from      = 'Web App';
        $address->save();

以下是迁移: 这是用户迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function($table){
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email');
        $table->string('phone_no', 20)->nullable();
        $table->string('password')->nullable();
        $table->string('remember_token', 100);
        $table->date('date_of_birth')->nullable();
        $table->string('created_from');

        $table->timestamps();
        $table->softDeletes();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}

}

地址

public function up()
{
    Schema::create('addresses', function($table){
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email');
        $table->string('address_line_1');
        $table->string('address_line_2')->nullable();
        $table->string('landmark')->nullable();
        $table->string('city')->nullable();
        $table->string('state')->nullable();
        $table->string('phone_no', 13);
        $table->integer('pincode');
        $table->string('created_from');

        $table->timestamps();
        $table->softDeletes();

        $table->foreign('user_id')->references('id')->on('users');
    });
}

如果有帮助,这里有错误的屏幕截图。 Screenshot of error

2 个答案:

答案 0 :(得分:0)

$table->integer('user_id')->unsigned();

$table->increments('id');

应该是相同的:unsigned

键应该是相同类型的数据

答案 1 :(得分:0)

根据错误消息,您的错误是由于在adress.user_id(FK到src.id)中插入一个值而导致的,该值在src.id中不存在。
在该示例中,您尝试在adresses.user_id中插入29,检查SELECT id FROM src WHERE id=29是否返回任何结果。如果没有,那就有问题了。