我正在使用 Laravels 的默认迁移来创建通知表。
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}
但是我在尝试使用它时遇到错误:
[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 `notifications` add index `n
otifications_notifiable_id_notifiable_type_index`(`notifiable_id`, `notifiable_type`))
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
更新
我将索引列的名称更改为notifiable_index,但它仍然提示索引键的长度。
[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 `notifications` add index `n
otifiable_index`(`notifiable_id`, `notifiable_type`))
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
最佳答案
如果您使用 Laravel 5.4并运行低于 5.7.7 版本的 MySQL 版本。您可以通过在 AppServiceProvider
类的 boot
方法中调用 Schema::defaultStringLength
方法来解决此问题。
public function boot()
{
Schema::defaultStringLength(191);
}
关于mysql - 创建通知表时 Laravel "Specified key was too long",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41600753/