在Laravel 5.4上使用php artisan make:auth迁移错误
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e users add unique users_email_unique(email))
[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
我已经解决了这个问题,并编辑我的config->database.php文件喜欢我的数据库('charset'=>'utf8')和('collation'=>'utf8_general_ci'),所以我的问题解决了如下代码:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
没有人告诉的解决方案是,在Mysql v5.5和以后的InnoDB是默认的存储引擎,它没有这个问题,但在许多情况下,像我的,有一些旧的Mysql ini配置文件使用旧的MYISAM存储引擎,如下所示。
default-storage-engine=MYISAM
这造成了所有这些问题,解决方案是将Mysql的ini配置文件中的默认存储引擎一劳永逸地更改为InnoDB,而不是做临时的hack。
default-storage-engine=InnoDB
如果你在MySql v5.5或更高版本,那么InnoDB是默认引擎,所以你不需要像上面那样显式地设置它,只要从你的ini文件中删除default-storage-engine=MYISAM(如果它存在),你就可以开始了。
如前所述,我们在App/Providers中添加到AppServiceProvider.php
use Illuminate\Support\Facades\Schema; // add this
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191); // also this line
}
你可以在下面的链接中看到更多细节(搜索“索引长度& MySQL / MariaDB”)
https://laravel.com/docs/5.5/migrations
BUT WELL THAT's not what I published all about! the thing is even when doing the above you will likely to get another error (that's when you run php artisan migrate command and because of the problem of the length, the operation will likely stuck in the middle. solution is below, and the user table is likely created without the rest or not totally correctly)
we need to roll back. the default roll back will not work. because the operation of migration didn't like finish. you need to delete the new created tables in the database manually.
我们可以这样做使用修补器如下所示:
L:\todos> php artisan tinker
Psy Shell v0.8.15 (PHP 7.1.10 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
我自己有一个问题与用户表。
之后你就可以开始了
PHP artisan migrate:回滚
PHP工匠迁移
我得到了这个错误尽管我已经有了(实际上因为我已经有了)
模式:defaultStringLength (191);在AppServiceProvider.php文件中。
原因是我试图在我的一个迁移中设置一个高于191的字符串值:
Schema::create('order_items', function (Blueprint $table) {
$table->primary(['order_id', 'product_id', 'attributes']);
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->string('attributes', 1000); // This line right here
$table->timestamps();
});
删除1000或将其设置为191解决了我的问题。