【问题标题】:Integrity constraint violation: 1452 Cannot add or update a child row完整性约束违规:1452 无法添加或更新子行
【发布时间】:2017-04-26 07:09:34
【问题描述】:

我收到了这个错误:

SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(pumma2.users,CONSTRAINT users_roles_id_foreign FOREIGN KEY(roles_id)参考users (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: 插入users (username, email, name, password, cover, roles_id, @9876543@4 987654335@) 值 (qweqwe, haikalhikmi@gmail.com, qweqweqew, $2y$10$YX2OPPTGEDcquYkgk.ln6eEuwtFqrtmRbIpvgEZqJJHR9gk8mfdfm, wallpaper-27058.jpg, 1, 2016-12-11 06:09:41, 06:0-2 :41))

这是我的注册控制器

$user=$request->file('cover');
        $destination ='img/user';
        $filename=$user->getClientOriginalName();
        storage::put('img/user/'.$filename,file_get_contents($request->file('cover')->getRealPath()));
 
        $user = new User();
                
        $user->username = $request->username;
        $user->email = $request->email;
        $user->name = $request->name;
        $user->password = bcrypt($request->password);
        $user->cover = $filename;
        $user->roles_id = DB::table('roles')->select('id')->where('rolename','user')->first()->id;       
                 
        $user->save();

当我尝试 dd($user):

它有效:

attributes: array:6 [▼
    "username" => "qweqwe"
    "email" => "haikalhikmi@gmail.com"
    "name" => "qweqweqew"
    "password" => "$2y$10$Ci5usZFP6ANCyBfQhN3Gzexh98Wb8R9n4AmVEvq8x/4bakfGF8l/y"
    "cover" => "wallpaper-27058.jpg"
    "roles_id" => 1
  ]

以防万一你想查看我的用户表

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('roles_id')->nullable();
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('name')->unique();
            $table->string('password');
            $table->string('cover');
            $table->rememberToken();
            $table->timestamps();
        });

        Schema::create('roles', function (Blueprint $table){
            $table->increments('id');
            $table->string('rolename');
        });

        schema::table('users', function (Blueprint $table){            
            $table->foreign('roles_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            
        });

    }

【问题讨论】:

    标签: mysql laravel-5


    【解决方案1】:

    从您上面的代码中,用户与角色具有belongTo 关系。所以用户外键的迁移应该是

    schema::table('users', function (Blueprint $table){            
            $table->foreign('roles_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
    
    });  
    

    roles_id 列应引用roles 表上的id 列,而不是users 表。

    更改外键应该可以帮助您摆脱约束冲突。

    更新(在下面的评论中对 OP 的查询进行更多解释)

    在问题中的RegisterController代码中

    $user->roles_id = DB::table('roles')->select('id')->where('rolename','user')->first()->id;  
    

    抛出异常,因为DB::table('roles')->select('id') - 这部分将返回一个整数,从这里链接到->where('rolename','user')->first()->id 就像试图在一个普通整数上查找一个属性,这将导致异常。

    所以查询可以定义为

    $user->roles_id = DB::table('roles')->where('rolename', 'user')->first()->id;
    
    /* 
        Or - to get just the id instead of the entire record 
     */ 
    
    $user->roles_id = DB::table('roles')->where('rolename', 'user')->value('id'); 
    
    /* 
        Or - if there is a Role model defined corresponding to the roles table 
     */
    
    $user->roles_id = \Role::where('rolename', 'user')->first()->id;
    

    【讨论】:

    • 试图在 RegisterController 第 48 行获取非对象的属性 ------------------ -------------------------------------------------- -- 第 48 行:$user->roles_id = DB::table('roles')->select('id')->where('rolename','user')->first()->id;
    • 您没有在项目中定义角色模型吗?使用 eloquent 可以使用 $user->roles_id = \Role::where('rolename', 'user')->first()->id;在您的查询中 $user->roles_id = DB::table('roles')->select('id') 只返回一个整数 id,继续在 id 上进行链接肯定不起作用并抛出异常。你可以有 $user->roles_id = DB::table('roles')->where('rolename', 'user')->first()->id;
    猜你喜欢
    • 2023-03-23
    • 2015-09-02
    • 1970-01-01
    • 2016-05-31
    • 2018-04-17
    • 2014-03-12
    • 2020-09-29
    • 2020-01-23
    相关资源
    最近更新 更多