【问题标题】:Issues when running php artisan migrate tables from laravel app to localhost database运行 php artisan 将表从 laravel 应用程序迁移到 localhost 数据库时的问题
【发布时间】:2020-09-25 22:12:35
【问题描述】:

以下:是我会收到的错误吗(请注意,如果它不是 users 表,它是 failed_jobs 或 reset_password 表。大多数问题来自 laravel 自动生成的表。是的,我确实尝试过 PHP artisan迁移:重置,从本地主机删除表,我什至删除了数据库本身)

Migrating: 2014_10_12_100000_create_password_resets_table

   Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `password_resets` add index `password_resets_email_index`(`email`))

  at C:\xampp\htdocs\project1\project\seed\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:\xampp\htdocs\project1\project\seed\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")

  2   C:\xampp\htdocs\project1\project\seed\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOStatement::execute()

这就是数据库/迁移/create_reset_password_table 到目前为止的样子。

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('password_resets');
    }
}

提前感谢您的所有帮助! :)

【问题讨论】:

    标签: laravel laravel-7 laravel-migrations


    【解决方案1】:

    Laravel 默认排序规则类型已更改为 utf8mb4。

    在你的

    AppServiceProvider.php

    引导方法如下一行

    use Illuminate\Support\Facades\Schema;
    
    public function boot()
    {
        Schema::defaultStringLength(191);
    }
    

    【讨论】:

    • 我之前已经尝试过了,根据别人的问题我尝试了以下方法:``` public function boot() { Schema::defaultStringLength(500); } ``` 之后我仍然会遇到同样的问题。 @AdnanMumtaz
    • 将默认长度设置为 191 或更改排序规则
    • 谢谢,成功了!在我的本地主机中删除所有表之后。
    • @Gambit 如果这个答案真的对你有帮助。您可以将其标记为正确
    猜你喜欢
    • 2019-05-24
    • 2016-05-22
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2018-08-01
    • 2018-11-01
    • 2017-06-11
    相关资源
    最近更新 更多