SQLSTATE [23000]:完整性约束违例:1452无法添加或更新子行:外键约束失败 - Laravel [英] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

查看:8779
本文介绍了SQLSTATE [23000]:完整性约束违例:1452无法添加或更新子行:外键约束失败 - Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过,但是我仍然找不到我做错了什么。

I know that this question has already been asked, but I still can't find what I'm doing wrong.

我使用框架Laravel。

I'm using the framework Laravel.

我有2个表(用户和位置)。当我要创建一个用户时,会收到错误信息:

I have 2 tables (Users and Locations). When I want to create a User, I get the error message:


SQLSTATE [23000]:完整性约束违反:1452无法添加或
更新子行:外键约束失败
festival_aid users ,CONSTRAINT fk_users_locations1 FOREIGN KEY
location_id )REFERENCES locations location_id )ON DELETE
CASCADE ON UPDATE NO ACTION)(SQL:insert into users c $ c> user_id ,
user_email location_id ,?,?))(Bindings:array(0 =>
'1',1 =>'test@hotmail.com',2 =>'1',))

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (festival_aid.users, CONSTRAINT fk_users_locations1 FOREIGN KEY (location_id) REFERENCES locations (location_id) ON DELETE CASCADE ON UPDATE NO ACTION) (SQL: insert into users (user_id, user_email, location_id) values (?, ?, ?)) (Bindings: array ( 0 => '1', 1 => 'test@hotmail.com', 2 => '1', ))

表用户

CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
  `user_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_email` VARCHAR(45) NOT NULL,
  `user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_modified` TIMESTAMP NULL,
  `user_deleted` TIMESTAMP NULL,
  `user_lastlogin` TIMESTAMP NULL,
  `user_locked` TIMESTAMP NULL,
  `location_id` BIGINT NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
  CONSTRAINT `fk_users_locations1`
    FOREIGN KEY (`location_id`)
    REFERENCES `festival_aid`.`locations` (`location_id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION,
ENGINE = InnoDB;

表位置

DROP TABLE IF EXISTS `festival_aid`.`locations` ;

CREATE TABLE IF NOT EXISTS `festival_aid`.`locations` (
  `location_id` BIGINT NOT NULL AUTO_INCREMENT,
  `location_latitude` FLOAT NOT NULL,
  `location_longitude` FLOAT NOT NULL,
  `location_desc` VARCHAR(255) NULL,
  `location_type` VARCHAR(45) NULL,
  PRIMARY KEY (`location_id`))
ENGINE = InnoDB;

迁移用户

public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->increments('user_id');
            $table->string('user_email');
            $table->timestamp('user_created');
            $table->timestamp('user_modified');
            $table->timestamp('user_deleted');
            $table->timestamp('user_lastlogin');
            $table->timestamp('user_locked');

            $table->foreign('location_id')
                ->references('id')->on('locations');
            //->onDelete('cascade');
        });
    }

迁移位置

public function up()
    {
        Schema::table('locations', function(Blueprint $table)
        {
            $table->primary('location_id');
            $table->float('location_latitude');
            $table->float('location_longitude');
            $table->string('location_desc');
            $table->string('location_type');
        });
    }

模型用户

public function location()
    {
        return $this->belongsTo('Location');
    }

模型位置

 public function user()
    {
        return $this->hasOne('User');
    }

控制器

public function store()
    {
        $input = Input::all();

        $rules = array('user_email' => 'required|unique:users|email');

        $v = Validator::make($input, $rules);

        if($v->passes())
        {
            $user = new User();
            $location = new Location();

            $user->user_email = $input['user_email'];
            //$user->location_id = $input['location_id'];

            $location->location_latitude = $input['location_latitude'];
            $location->location_longitude = $input['location_longitude'];

            $user->save();
            $location->save();
    }

我似乎找不到我做错了。显然外键有问题。

I can't seem to find what I'm doing wrong. Obviously there is something wrong with the foreign key.

推荐答案

用户行需要引用一个有效的位置。
该位置必须在用户保存前可用。
因此,首先保存位置,然后保存用户并取消注释 $ user-> location_id 行,您就完成了。

A user row needs a reference to a valid location. That location must be available before the user is saved. So, first save the location and then save the user and uncomment the $user->location_id line and you are done.

这篇关于SQLSTATE [23000]:完整性约束违例:1452无法添加或更新子行:外键约束失败 - Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆