I'm rather new to Laravel; I dabbled with it a bit in the spring but now I'm back and trying to understand it better, particularly how Laravel and Vue work together. In that spirit, I'm trying to do this [tutorial][1] but I'm having trouble with my php artisan migrate in Step 4.
This is the error I'm getting:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`))
I think this is a big clue that I need to change the definition of the failed_jobs table to include an additional unique index on the uuid column; they want me to call the index failed_jobs_uuid_unique. The problem is that I don't know who to write that. I can't find an example anywhere, aside from one [here][2]. I've gone into 2019_08_19_000000_create_failed_jobs_table.php and tried to imitate the example for my circumstances. I came up with this as my revised up() method:
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
Schema::table('failed_jobs', function (Blueprint $table) {
DB::statement('ALTER TABLE `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`)');
});
}
I've just added the second statement and left the first statement alone. This is the output I get:
php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (492.69ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (647.70ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`))
at C:\Laravel\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
667▕ // If an exception occurs when attempting to run a query, we'll format the error
668▕ // message to include the bindings with SQL, which will make this exception a
669▕ // lot more helpful to the developer instead of just the database's errors.
670▕ catch (Exception $e) {
➜ 671▕ throw new QueryException(
672▕ $query, $this->prepareBindings($bindings), $e
673▕ );
674▕ }
675▕
1 C:\Laravel\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
2 C:\Laravel\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOStatement::execute()
In other words, I'm getting exactly the same error as before. What do I need to do differently to solve this problem? I assume I've just mangled the syntax of the alter statement somehow but I'm really not sure what I've done wrong.
Would it be better to modify the original statement in the up() method to make the table include the additional index? The uuid column already has a unique index on it but maybe it's important that the name of the index is failed_jobs_uuid_unique; if that's the case, how would the original statement change?
[1]: https://www.tutsmake.com/larave-vue-js-spa-crud-example-tutorial/
[2]: https://stackoverflow.com/questions/44200696/execute-multiple-laravel-alter-table-migration-queries-in-one