【问题标题】:Laravel migration: Specified key was too long; max key length is 767 bytesLaravel 迁移:指定的键太长;最大密钥长度为 767 字节
【发布时间】:2019-11-11 16:06:04
【问题描述】:

我有这样的迁移:

    Schema::table('table', function (Blueprint $table) {
        $table->string('description', 500)->index()->change();
    });

但我收到此错误:

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

我尝试过的:

(1) 将->index() 更改为index("t_index")。同样的错误。 (2) 在AppServiceProvider

public function boot()
{
    Schema::defaultStringLength(512);
}

同样的错误。

不知道该怎么办。我在本地环境中没有收到错误,只是现在我正在尝试部署。

【问题讨论】:

  • 尝试在您的服务器上安装更新的 mysql 版本(MySQL
  • 或者,仅索引文本的前 x 个字符。你需要使用类似DB::statement('ALTER TABLE table ADD INDEX description (description(10));')

标签: mysql laravel


【解决方案1】:

您应该更改类型字段,因为类型 string 的最大长度为 256。 请在textlongtext 上更改类型

例如:

 Schema::table('table', function (Blueprint $table) {
        $table->text('description', 500)->index()->change();
    });

你应该阅读这篇文章 https://laravel.com/docs/5.8/migrations#creating-columns

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2012-05-31
    • 2015-06-22
    • 1970-01-01
    • 2013-12-21
    • 2015-06-29
    • 2016-10-04
    • 2014-08-17
    • 2017-11-23
    相关资源
    最近更新 更多