【问题标题】:Laravel 5.6: Table creation failedLaravel 5.6:表创建失败
【发布时间】:2018-07-22 12:25:12
【问题描述】:

我是 Laravel 的新手,我正在尝试使用 Schema 外观创建表。我用命令创建迁移文件

php artisan make:migration create_products_define_standards_table --create=products_define_standards

这是文件:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateStandardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products_define_standards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code')->unique();
            $table->string('image');
            $table->timestamps();
        });
    }

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

它确实与默认CreateUsersTable 的内容几乎相同,但是当我运行php artisan migrate 时,它会创建:

  • users' 表(默认)
  • migrations' 表(默认)

不是

  • password_resets' 表(默认)
  • products_define_standards' 表(自定义)

我尝试使用 php artisan migrate:fresh,但得到相同的日志:

Dropped all tables successfully.
Migration table created successfully.

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

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

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
      /home/whatever/whatever.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php : 458

  2   PDOStatement::execute()
      /home/whatever/whatever.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php : 458

我找到了this answer,我也运行了composer dumpauto,但结果是一样的。

我错过了什么吗?我应该做任何其他事情来在其他地方注册迁移吗?

【问题讨论】:

    标签: php laravel laravel-5 database-migration laravel-5.6


    【解决方案1】:

    编辑位于app/Providers 目录中的AppServiceProvider.php,并在boot() 方法中设置默认字符串长度。

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

    【讨论】:

    • Great Sapnesh,现在所有的迁移都完美运行了。所以问题是 Laravel 没有为VARCHAR 提供默认字符串的长度,而 MySQL 抛出了错误?
    • @Brigo 是的,类似的。但是如果你想要一个彻底的解释,请看这个问题stackoverflow.com/questions/43832166/…
    • @Brigo 如果对你有帮助,请采纳答案:)
    • 当然,我在等待“锁定时间”到期 :) 感谢您提供指向其他答案的链接
    【解决方案2】:

    在 5.6 版中,您应该编辑 2 个文件:

    第一

    编辑位于 app/Providers 目录中的 AppServiceProvider.php,并在 boot() 方法中设置默认字符串长度。

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

    第二

    编辑位于 config/database.php 目录中的 database.php

    在mysql配置部分'engine'为空,你应该用'InnoDB ROW_FORMAT=DYNAMIC'替换

    希望你喜欢。

    【讨论】:

      【解决方案3】:

      这里的工作方法是传递带有键名的第二个参数(一个短的):

      $table->string('code')->unique(null,'unikcode');
      

      【讨论】:

        【解决方案4】:

        我想针对这个问题提出一个可能更好的解决方案。

        您不必编辑 Laravel 中的任何文件。相反,请编辑您的实际数据库排序规则和引擎。

        我假设您使用的是 MySQL 或 MariaDB。使用 phpMyAdmin,当您创建空白数据库时,请使用 utf8mb4_unicode_ci(utf8_unicode_ci 或 utf8P_general_ci 也可以正常工作)。

        接下来将您的默认数据库引擎设置为 InnoDB 而不是 MyISAM(您也可以在“变量”选项卡下的 phpMyAdmin 中执行此操作,搜索“引擎”。

        人们提出的其他解决方案 - 即编辑 database.php 以使其无论如何都使用 InnoDB,具有非常相似的效果。但无论如何,现代版本的 MySQL/Maria应该使用 InnoDB。

        运行您的迁移,无需进一步修改即可正常执行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-29
          • 2013-09-14
          • 2014-05-22
          • 1970-01-01
          • 2018-10-31
          • 1970-01-01
          相关资源
          最近更新 更多