加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – SQLSTATE [23000]:完整性约束违规:1452无法添加或更新

发布时间:2020-12-14 19:45:33 所属栏目:大数据 来源:网络整理
导读:我知道这个问题已被提出,但我仍然无法找到我做错的事. 我正在使用Laravel框架. 我有2个表(用户和位置).当我想创建一个用户时,我收到错误消息: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key con
我知道这个问题已被提出,但我仍然无法找到我做错的事.

我正在使用Laravel框架.

我有2个表(用户和位置).当我想创建一个用户时,我收到错误消息:

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

我似乎无法找到我做错的事.显然外键有问题.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读