You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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');
}
}
The text was updated successfully, but these errors were encountered:
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.
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 thetokenable_type
as a string with the desired length andtokenable_id
as an unsigned big integer. After defining the columns, we add the index separately to ensure it's created with the correct length.}
The text was updated successfully, but these errors were encountered: