Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Specified key length exceeds maximum limit during migration #2

Closed
HMAHD opened this issue Sep 3, 2023 · 1 comment
Closed

Comments

@HMAHD
Copy link

HMAHD commented Sep 3, 2023

Bug Description:

When attempting to create a migration for the personal_access_tokens table in Laravel, a "Specified key was too long" error occurs. This error arises because Laravel tries to create an index with a key length that exceeds the maximum allowed by the database engine (MySQL), causing the migration to fail.

Fixed Code:

The issue can be resolved by adjusting the migration code to specify a custom index name with a shorter key length for the tokenable_type column. Additionally, we explicitly define the tokenable_type as a string with the desired length and tokenable_id as an unsigned big integer. After defining the columns, we add the index separately to ensure it's created with the correct length.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePersonalAccessTokensTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('personal_access_tokens', function (Blueprint $table) {
            $table->id();
            
            // Use a smaller index key length
            $table->string('tokenable_type', 191); // Adjust the length as needed
            $table->unsignedBigInteger('tokenable_id');
            
            $table->string('name');
            $table->string('token', 64)->unique();
            $table->text('abilities')->nullable();
            $table->timestamp('last_used_at')->nullable();
            $table->timestamps();

            // Add an index separately
            $table->index(['tokenable_type', 'tokenable_id']);
        });
    }

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

}

@HMAHD HMAHD closed this as completed Sep 3, 2023
@opariltay
Copy link
Owner

Hey @HMAHD ,

Thanks for raising that issue. According to official Laravel documentation, this happens when you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release.

Please see below pages for detailed explanation:
https://laravel.com/docs/8.x/migrations#index-lengths-mysql-mariadb
https://stackoverflow.com/a/42245921

Anyone with an older MySQL or MariaDB release can refer to this issue in the future.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants