laravel-mysql-partition icon indicating copy to clipboard operation
laravel-mysql-partition copied to clipboard

unique constraint Error

Open latif-developers-studio opened this issue 3 years ago • 6 comments

If I add any unique constraint then

General error: 1503 A UNIQUE INDEX must include all columns in the table's partitioning function

I got this error.

Here is my migration

Schema::create('cp_transactions', function (Blueprint $table) {

        $table->bigInteger('id');

        $table->primary(['id','date']);

        $table->string('trx_id')->index('trx_id')->unique();

        $table->date('date')->index('date')->nullable();

        $table->timestamps();

    });

    Schema::forceAutoIncrement('cp_transactions', 'id');

    Schema::partitionByYearsAndMonths('cp_transactions', 'date', 2019);

Can you please help me to resolve this issue?

latif-developers-studio avatar Mar 14 '22 14:03 latif-developers-studio

Same error

sts-ryan-holton avatar Dec 13 '23 11:12 sts-ryan-holton

getting same error by implementing unique check on any db column

workOfDsYasir avatar Dec 15 '23 07:12 workOfDsYasir

Same error

@sts-ryan-holton, @workOfDsYasir

Yes, there is a constraint by MySQL end. If you are going to apply a database partition then you are unable to add the unique index.

latif-developers-studio avatar Dec 15 '23 07:12 latif-developers-studio

How did you resolve it? It seems the docs: $table->primary(['id','date']); doesn't work

sts-ryan-holton avatar Dec 15 '23 11:12 sts-ryan-holton

How did you resolve it? It seems the docs: $table->primary(['id','date']); doesn't work

You have to create composite key rather than primary key and to create composite key $table->primary(['id','date']); this is the way.

Laravel 10: composite key

workOfDsYasir avatar Dec 15 '23 12:12 workOfDsYasir

This isn't working lol/

sts-ryan-holton avatar Dec 15 '23 12:12 sts-ryan-holton