Laravel5.6 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max k

个人博客,防止重复造轮子

Laravel 5.6 在执行 php artisan migrate可能会出现如下报错

 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 `users` add unique `users_email_unique`(`email`))

  at D:\Oscar\htdocs\phili\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
      D:\Oscar\htdocs\phili\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:144

  2   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
      D:\Oscar\htdocs\phili\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:142

  Please use the argument -v to see more details.

解决方法

在app/Providers/AppServiceProvider.php中修改
use Illuminate\Support\Facades\Schema;
public function boot()
    {
        //
      Schema::defaultStringLength(191);
    }
然后再执行 php artisan migrate
如果报错
  Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists 
是由于上一步已经生成了几张表,把生成的表删除,重新执行php artisan migrate即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值