Laravel 执行 migrate 数据库迁移报错问题解决

mysql 5.5和5.6 执行数据库迁移报错问题

执行 php artisan migrate 时报错:

Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL
: alter table `users` add unique `users_email_unique`(`email`))

  at K:\temp\000\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
      K:\temp\000\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463

  2   PDOStatement::execute()
      K:\temp\000\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463

  Please use the argument -v to see more details.

原因

框架自带的迁移文件 2014_10_12_000000_create_users_table 执行迁移的时候有个字段 email 没有指定varchar长度,所以默认长度会是 255,然而执行创建索引 users_email_unique 时不允许这么长

解决

修改文件 app\Providers\AppServiceProvider.php 内容如下:

use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Schema::defaultStringLength(150);
    }
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值