laravel 错误 1071 密钥太长 php artisan migrate [重复]

Posted

技术标签:

【中文标题】laravel 错误 1071 密钥太长 php artisan migrate [重复]【英文标题】:laravel error 1071 key was too long php artisan migrate [duplicate] 【发布时间】:2020-11-10 20:45:02 【问题描述】:

我正在使用 Laravel,并且我有功能迁移:

public function up()
    
        Schema::create('products', function (Blueprint $table) 
            $table->increments('id');
            $table->string('title')->unique();
            $table->string('slug')->unique();
            $table->string('subtitle');
            $table->integer('price');
            $table->text('description');
            $table->boolean('featured')->default(false);
            $table->timestamps();
 
        );
                

当我在我的 cmd 上执行“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 La clé est trop longue. Longueur maximale: 1000 (SQL: alter table `users` add unique `users_username_unique`(`username`))

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

  1   C:\Users\linda\lynda-master\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 La clé est trop longue. Longueur maximale: 1000")

  2   C:\Users\linda\lynda-master\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOStatement::execute()                                  

我将它添加到我的 AppServiceProvider 中

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    
        //
    

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    
        Schema::defaultStringLength(191);
    
                                    

这是我的database.php

 'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'laravel-ecommerce'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => 'InnoDB',
        
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ], 

                                

我的 .env 看起来像

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-ecommerce
DB_USERNAME=root
DB_PASSWORD=                                   

我已经在我的 php.ini 中取消注释扩展名 ;extension=pdo_mysql 我正在使用 wamp 服务器并且已经尝试过:

php artisan cache:clear                    
php artisan config:clear                      
php artisan config:cache 

当我使用 mariaDB 而不是 mysql 创建我的数据库时,我收到了这个错误 未知数据库

 Illuminate\Database\QueryException

  SQLSTATE[HY000] [1049] Base 'laravel-ecommerce' inconnue (SQL: select * from information_schema.tables where table_schema = laravel-ecommerce and table_name = migrations and table_type = 'BASE TABLE')

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

  1   C:\Users\linda\lynda-master\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDOException::("SQLSTATE[HY000] [1049] Base 'laravel-ecommerce' inconnue")

  2   C:\Users\linda\lynda-master\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=laravel-ecommerce", "root", "", [])

【问题讨论】:

【参考方案1】:

这个问题可能是由于 Mariadb 或旧版本的 MySQL 造成的。

解决方案 1:

编辑位于/app/Providers/AppServiceProvider.phpAppServiceProvider.php 文件,并在启动方法中更新默认字符串长度:

// import builder where defaultStringLength method is defined
use Illuminate\Support\Facades\Schema;

public function boot()

    // Fix for MySQL < 5.7.7 and MariaDB < 10.2.2
    Schema::defaultStringLength(191); //Update defaultStringLength

解决方案 2:

把Mariadb改成mysql,也可以解决问题。

解决方案 3:

config/database.php里面,把这一行换成mysql

'engine' => null',

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

希望其中一种解决方案对您有用!!

【讨论】:

感谢您的回复,但是当我按照您所说的进行操作时,laravel 出现错误:public function boot() defaultStringLength; 那么什么对你有用? 我已经改成 mariaDB 但它给了我未知的数据库 我通过更改mysql的版本解决了这个问题,希望有一天能对某人有所帮助,非常感谢

以上是关于laravel 错误 1071 密钥太长 php artisan migrate [重复]的主要内容,如果未能解决你的问题,请参考以下文章

php 语法错误或访问冲突:1071指定密钥太长;最大密钥长度为767字节

语法错误或访问冲突:1071 指定的密钥太长;最大密钥长度为 767 字节 [重复]

Laravel 5.4:指定的密钥太长错误[重复]

为啥会出现此错误 #1071 - 指定的密钥太长;最大密钥长度为 1000 字节? [复制]

错误:#1071 - 指定的密钥太长;最大密钥长度为 1000 字节 - mysql 5.0.91

SQL 错误 #1071 - 指定的键太长;最大密钥长度为 767 字节