Home > Software engineering >  Laravel migration "Identifier name is too long"
Laravel migration "Identifier name is too long"

Time:05-29

Migrating: 2019_12_14_000001_create_personal_access_tokens_table

Illuminate\Database\QueryException

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'gjsdjuejwpls_personal_access_tokens' already exists (SQL: create table gjsdjuejwpls_personal_access_tokens (id bigint unsigned not null auto_increment primary key , tokenable_type varchar(255) not null, tokenable_id bigint unsigned not null, name varchar(255) not null, token varchar(32) not null, abilities text null, last_used_at timestamp null, created_at timestamp null, updat ed_at timestamp null) default character set utf8 collate 'utf8_unicode_ci')

Please tell me how to fix this error. Vedas this migration at once at installation costs.

public function up()
{
    Schema::create('personal_access_tokens', function (Blueprint $table) {
        $table->id();
        $table->morphs('tokenable');
        $table->string('name');
        $table->string('token', 64)->unique();
        $table->text('abilities')->nullable();
        $table->timestamp('last_used_at')->nullable();
        $table->timestamps();
    });
}

CodePudding user response:

You can provide a shorter index name as second argument to the morphs() in the migration

public function up()
{
    Schema::create('personal_access_tokens', function (Blueprint $table) {
        $table->id();

        // Provide shorter index name as the second argument
        $table->morphs('tokenable', 'personal_access_tokenable_index');
        $table->string('name');
        $table->string('token', 64)->unique();
        $table->text('abilities')->nullable();
        $table->timestamp('last_used_at')->nullable();
        $table->timestamps();
    });
}

MySQL has a limit of 64 characters for the length of index names - (refer the link posted by Bill Karwin in comment below). By default Laravel uses table_name_column(s)_name(s)_index as the format for identifier (index names). So if it exceeds the limit of 64 characters then it will throw an error

CodePudding user response:

The table already exists you can use php artisan migrate:fresh to re-run the migration but Keep in mind that it deletes and rebuilds all your tables and data.

  • Related