首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >laravel错误1071键太长的php手工迁移

laravel错误1071键太长的php手工迁移
EN

Stack Overflow用户
提问于 2020-07-21 12:56:48
回答 1查看 10.2K关注 0票数 1

我使用的是Laravel,我具有以下功能:

代码语言:javascript
复制
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迁移‘时,我会得到错误

代码语言:javascript
复制
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里了

代码语言:javascript
复制
<?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

代码语言:javascript
复制
 '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看起来就像

代码语言:javascript
复制
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-ecommerce
DB_USERNAME=root
DB_PASSWORD=                                   

我已经在我的php.ini中取消了扩展php.ini的注释,我正在使用wamp服务器,并且已经尝试了以下操作:

代码语言:javascript
复制
php artisan cache:clear                    
php artisan config:clear                      
php artisan config:cache 

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

代码语言:javascript
复制
 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", "", [])
EN

回答 1

Stack Overflow用户

发布于 2020-07-21 13:04:10

此问题可能是由于Mariadb或旧版本的MySQL引起的。

解决方案1:

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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
'engine' => null',

使用

代码语言:javascript
复制
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

希望其中一个解决方案对你有用!

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63015038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档