PHP artisan迁移不会迁移所有表

时间:2019-11-27 08:33:55

标签: php mysql laravel

我正在尝试将表迁移到数据库。我正在上Laravel课程,但出现错误。

C:\laragon\www\blog                                                                                                                              
λ php artisan migrate                                                                                                                            
Migration table created successfully.                                                                                                            
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 76
7 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))                                                                      

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

  Exception trace:                                                                                                                               

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")          
      C:\laragon\www\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459                                                    

  2   PDOStatement::execute()                                                                                                                    
      C:\laragon\www\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459                                                    

  Please use the argument -v to see more details.                                                                                                

C:\laragon\www\blog                                                                                                                              
λ                                                                                                                                                

我已经在PHPMyAdmin中创建了一个数据库,当我执行迁移时,它会从4个表中迁移2个,这意味着它们在.env文件上已正确连接。我试图通过PHPMyAdmin手动添加那些表,但是在此之后,当我制作工厂文件和播种器时,我遇到了不同的错误,因此我陷入了困境。

在我跑步之前:

λ php artisan make:migration create_todos_table
Created Migration: 2019_11_27_081314_create_todos_table

一切正常,但是在迁移时出现错误。谢谢

2 个答案:

答案 0 :(得分:0)

编辑您的AppServiceProvider.php文件,并在启动方法中设置默认字符串长度:

转到App\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

然后运行

php artisan migrate

答案 1 :(得分:0)

在您的App \ Providers \ AppServiceProvider.php中

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

然后运行

php artisan migrate

请参考

[https://laravel-news.com/laravel-5-4-key-too-long-error]

相关问题