kikter's avatar

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`seller`.`sellers`, CONSTRAINT `sellers_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE)

please how do i resolve this issue , the user table is in another database different from the seller database

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`seller`.`sellers`, CONSTRAINT `sellers_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE)

my livewire

 $user = User::where('email', $this->email)->first();

        if (!$user) {
            // $studentNumber = $this->generateUniqueStudentNumber();

        
            $user = User::on('second_db')->create([
                'name' => $this->name,
                
                'email' => $this->email,
                'password' => bcrypt($this->password), // Correctly hash the password
            ]);
        }
        

        if (!$this->isStudent) {
            $this->validate([
                'nin' => 'required',
                'state' => 'required',
                'picture' => 'required|image',
            ]);
        } else {
            $this->validate([
                'department' => 'required',
                'schoolIdCard' => 'required|image',
            ]);
        }

       

        // Upload school ID card image
        if ($this->isStudent) {
            $schoolIdCardPath = $this->schoolIdCard->store('public/school_id_cards');
        } else {
            $picturePath = $this->picture->store('public/seller_pictures');
            $schoolIdCardPath = null;
        }


        // Save seller to the database
        // Seller::create([
        //     'user_id' => $user->id,
        //     'is_student' => $this->isStudent,
        //     'department' => $this->department,
        //     'school' => $this->school,
        //     'school_id_card' => $schoolIdCardPath,
        //     'picture' => $picturePath,
        //     'state_id' => $this->state,
        //     'business_name' => $this->business,
        //     'nin' => $this->nin,
        //     'address' => $this->address,
        //     'whatsapp_number' => $this->whatsappNumber,
        //     'verification_status' => false,
        // ]);

        $seller = new Seller();
        $seller->setAttribute('user_id', $user->id);
        $seller->setAttribute('state_id', $this->state);
        $seller->is_student = $this->isStudent;
        $seller->department = $this->department;
        $seller->school = $this->school;
        $seller->school_id_card = $schoolIdCardPath;
        $seller->picture = $picturePath;
       
        $seller->business_name = $this->business;
        $seller->nin = $this->nin;
        $seller->address = $this->address;
        $seller->whatsapp_number = $this->whatsappNumber;
        $seller->verification_status = 0;
        $seller->save();

my model

class Seller extends Model
{
    use HasFactory;
    protected $connection = 'mysql';
    protected $guarded = [];
   


    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
0 likes
5 replies
geraintp's avatar

Im guessing its related to this? $user = User::on('second_db')->create([

thats a different database, so you primary database is not going to have any record of that user, so that ID will be invalid hence the constraint violation.

that DB constrain means that the user_id passed to it must exist in that SAME databases user table.

kikter's avatar

@geraintp I just need a solution on how to solve it , I am guessing they suppose to be a way to bypass this

geraintp's avatar

@kikter ? why would there be a way to bypass this?? there is a constraint on the database table for a reason, your database schema (design) says that a seller record can not exist without a valid user record.

You could remove the constrain from your db schema IF, thats what you actually want to do and it makes sense for you to do that (which it probably doesn't).

but most likely your entire app and database design need a rethink and a carefully considered plan.

  • Changing the schema, Making user optional in for sellers in db 1 is one option.
  • Adding the user to db 1 as well as db 2 if they don't exist is another. (with a reference to its external db 2's id) is another.

but I've got no idea if either of those are a good idea's in the context of your app as i've got no idea of the consequences of either of those actions and again guessing here but I'm betting both are probably bad ideas and are going to cause you issues else where.

Snapey's avatar

so you have a users table on THIS database and OTHER database

suppose you were able to save this value, how would anyone know which database the id was valid for ?

This is a question of redesign of your app, not some technical workaround

Please or to participate in this conversation.