Home > Software design >  SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined

Time:10-19

I'm trying to switch my application to use Uuid, but my migration is failing on events table.

SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table events add primary key events_id_primary(id))

Schema::create('events', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('title', 255);
        $table->integer('join_id', 6)->unique();
        $table->string('image')->nullable();
        $table->string('location')->nullable();
        $table->text('description')->nullable();
        $table->timestamp('event_date')->nullable();
        $table->timestamp('event_time')->nullable();
        $table->foreignUuid('user_id')->nullable()->index();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->softDeletes();
        $table->timestamps();
    });


Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('name')->nullable();
        $table->string('email')->unique();
        $table->string('username')->nullable();
        $table->string('avatar')->nullable();
        $table->string('phone_number')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

Any help is greatly appreciated!

CodePudding user response:

When you check the documentation for integer() you see that the second parameter is not for the size, it's for the auto-increment/primary of the field. integer type is in itself a size (tinyInteger, smallInteger, bigInteger, ...). And the 6 turned into boolean returns true.

/**
     * Create a new integer (4-byte) column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Database\Schema\ColumnDefinition
     */
    public function integer($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
    }

You just need to remove that 6

Schema::create('events', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('title', 255);
        $table->integer('join_id')->unique();
        $table->string('image')->nullable();
        $table->string('location')->nullable();
        $table->text('description')->nullable();
        $table->timestamp('event_date')->nullable();
        $table->timestamp('event_time')->nullable();
        $table->foreignUuid('user_id')->nullable()->index();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->softDeletes();
        $table->timestamps();
    });
  • Related