Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

julianov's avatar

SQLSTATE[23505]: Unique violation in different tables

Hello. I've defined two tables users and user_confirmation_codes but I'm having this input when there is an insert

"SQLSTATE[23505]: Unique violation: 7 ERROR: Duplicate key violates unique constraint «users_idx_unique»\

The problem is that I need in both tables that the idx is unique but in those tables.

Why am I getting this error (Unique violation: 7 ERROR: ) if they are different tables?

user_confirmation_codes

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_confirmation_codes', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('idx')->unique();
            $table->integer('code');
            $table->timestamp('created_at')->nullable();

        });
    }

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

users:

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
              
            $table->id();
            $table->bigInteger('idx')->unique();
            $table->string('name');
            $table->string('email');
            $table->string('password');

            $table->timestamp('email_verified_at')->nullable();

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

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

This is the controller method:

public function singup(Request $request)
    {

        try {
            $validated = $this->validate($request, [
                'idx' => 'required',
                'name' => 'required',
                'email' => 'required',
                'password' => 'required',
            ]);

            $user = new User();
            $user->idx = $validated['idx'];
            $user->name = $validated['name'];
            $user->email = $validated['email'];

            $user->password = bcrypt($validated['password']);
            $user->save();

            $code = random_int(1000,9999);
            $validation_code = new UserConfirmationCode();
            $validation_code->idx = $validated['idx'];
            $validation_code->code = $code;

            $validation_code->created_at = Carbon::now()->timestamp;
            $validation_code->save();

            Mail::to('[email protected]')
            ->cc('[email protected]')
            ->queue((new EmailConfirmation($user , $code))->from('[email protected]', 'Laravel'));

            return response()->json([
                'status' => true,
                'message' => 'Email Sent',
            ], 201);

        } catch (\Throwable $th) {
            return response()->json([
                'status' => false,
                'message' => $th->getMessage()
            ], 500);
        }

    }
0 likes
3 replies
julianov's avatar

@Sergiu17 Thanks, so what column identifier should I use to make the fields value unique only and exclusively in the table? Or is there no way and the only thing that can be done is to call the identifiers of columns in different tables differently if we want them to be unique key?

Sergiu17's avatar

@julianov if you really want idx to be unique, then nothing to do here, just add some validation

'idx' => 'required|unique:users,unique:user_confirmation_codes',

Please or to participate in this conversation.