0

I am trying to migrate tables to the database. I am doing a course on Laravel and i got an error.

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                                                                                                                              
λ                                                                                                                                                

I have created a database in PHPMyAdmin and when I perform migration it migrates 2 from 4 tables which means that they are connected properly on .env file. I tried to add those tables manually through PHPMyAdmin but after that when I make factory file and seeder I different error so I am stuck on this.

Before that i ran:

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

and all went ok but when migrating i got an error. Thanks

Ahmed Ali
  • 1,908
  • 14
  • 28
Zoran Relić
  • 45
  • 1
  • 12

1 Answers1

0

edit your AppServiceProvider.php file and inside the boot method set a default string length:

Go to App\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

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

and then run

php artisan migrate
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49