【问题标题】:How do I alter a table in Laravel 8.x? [duplicate]如何更改 Laravel 8.x 中的表格? [复制]
【发布时间】:2021-03-11 22:37:53
【问题描述】:

我对 Laravel 比较陌生;我在春天涉足了一点,但现在我回来了,试图更好地理解它,特别是 Laravel 和 Vue 如何协同工作。本着这种精神,我正在尝试执行此操作 [tutorial][1],但在第 4 步中我的 php artisan 迁移遇到了问题。

这是我得到的错误:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`))

我认为这是一个很大的线索,我需要更改 failed_jobs 表的定义以在 uuid 列上包含一个额外的唯一索引;他们希望我调用索引 failed_jobs_uuid_unique。问题是我不知道该给谁写。除了 [这里][2] 之外,我在任何地方都找不到示例。我已经进入 2019_08_19_000000_create_failed_jobs_table.php 并尝试根据我的情况模仿这个例子。我想出了这个作为我修改后的 up() 方法:

public function up()
{
    Schema::create('failed_jobs', function (Blueprint $table) {
        $table->id();
        $table->string('uuid')->unique();
        $table->text('connection');
        $table->text('queue');
        $table->longText('payload');
        $table->longText('exception');
        $table->timestamp('failed_at')->useCurrent();
    });

    Schema::table('failed_jobs', function (Blueprint $table) {
        DB::statement('ALTER TABLE `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`)');
    });
}

我刚刚添加了第二条语句,而没有单独使用第一条语句。这是我得到的输出:

    php artisan migrate
    Migration table created successfully.
    Migrating: 2014_10_12_000000_create_users_table
    Migrated:  2014_10_12_000000_create_users_table (492.69ms)
    Migrating: 2014_10_12_100000_create_password_resets_table
    Migrated:  2014_10_12_100000_create_password_resets_table (647.70ms)
    Migrating: 2019_08_19_000000_create_failed_jobs_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 `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`))

    at C:\Laravel\blog\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:\Laravel\blog\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:\Laravel\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
      PDOStatement::execute()

换句话说,我得到的错误与以前完全相同。我需要做些什么来解决这个问题?我假设我只是以某种方式破坏了 alter 语句的语法,但我真的不确定我做错了什么。

修改 up() 方法中的原始语句,使表包含附加索引会更好吗? uuid 列上已经有一个唯一索引,但索引的名称可能很重要 failed_jobs_uuid_unique;如果是这样的话,原来的说法会如何变化?

  [1]: https://www.tutsmake.com/larave-vue-js-spa-crud-example-tutorial/
  [2]: https://stackoverflow.com/questions/44200696/execute-multiple-laravel-alter-table-migration-queries-in-one

【问题讨论】:

  • 你的 MySQL 是什么版本?此外,您链接的教程与失败的作业表无关。我失败的作业迁移没有 UUID 列。
  • 您已经在创建表$table->string('uuid')->unique()的过程中将字段设置为唯一。不需要第二部分。
  • @mikeroq - 我的 MySQL 服务器版本:10.1.38-MariaDB - mariadb.org 二进制发行版。是的,该教程并不真正相关,我只是提到它来说明我是如何到达我现在的位置的:php artisan migrate 正在爆炸。
  • @tpojka - 我在 uuid 上看到了唯一索引,但是当我运行 up() 方法不变时,它不起作用。我认为错误消息告诉我需要在 php artisan migrate 满意之前更改表。但我无法让该建议发挥作用。
  • 为什么$table->string('uuid')->unique()不够好?

标签: php laravel


【解决方案1】:

试试这个方法:

public function up()
{
    Schema::create('failed_jobs', function (Blueprint $table) {
        $table->id();
        $table->string('uuid');// remove constraint from here
        $table->text('connection');
        $table->text('queue');
        $table->longText('payload');
        $table->longText('exception');
        $table->timestamp('failed_at')->useCurrent();
    });

    Schema::table('failed_jobs', function (Blueprint $table) {
        $table->unique(['uuid'], 'failed_jobs_uuid_unique');// setting custom name for key
    });
}

public function down()
{
    Schema::disableForeignKeyConstraints();
    Schema::table('failed_jobs', function(Blueprint $table) {
        $table->dropUnique('failed_jobs_uuid_unique');
    });
    Schema::drop('failed_jobs');
    Schema::enableForeignKeyConstraints();
}

DocsCode.

如果您仍然收到 specified key is too long 错误消息,请将列类型从 string 更改为 text。而且,还有uuid 字段类型,所以你也可以在我上面链接的迁移页面上查看它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多