【问题标题】:Change string to text type in Laravel migration在 Laravel 迁移中将字符串更改为文本类型
【发布时间】:2018-06-18 02:24:50
【问题描述】:

我已经创建了一个表 stack_links 如下

Schema::create('stack_links', function (Blueprint $table) {
            $table->increments('id');
            $table->string('site_key');
            $table->string('url');
            $table->string('path_run')->nullable();
            $table->integer('state');
            $table->integer('parent')->nullable();
            $table->text('data');
            $table->timestamps();

            $table->unique(['site_key', 'url']);

            $table->index(['site_key']);
            $table->index(['state']);
            $table->index(['parent']);
            $table->index(['url']);
        });

现在我想将 url 列更改为文本类型。所以我试过这个

 $table->text('url')->change();

如何返回这个错误

1170 BLOB/TEXT column 'url' used in key specification without a key length

经过搜索,我发现错误是因为 url 被索引(MySQL error: key specification without a key length)。所以我删除了索引,但它仍然是同样的错误。我关注:https://laravel.com/docs/5.5/migrations#dropping-indexes

Schema::table('stack_links', function (Blueprint $table) {
            $table->dropUnique(['url']);
            $table->dropIndex(['url']);

            $table->text('url')->change();
            $table->string('md5_url');

            $table->unique('md5_url');
            $table->index('md5_url');
        });

谁能告诉我哪里错了。

更新:问题出在dropIndex的参数上。当我用索引的名称替换它时。它的工作原理

【问题讨论】:

  • stackoverflow.com/questions/1827063/… 关注这个我想这会对你有所帮助。
  • @KuldeepMishra 我删除了索引,但它仍然记录相同的错误
  • laravel.com/docs/5.5/migrations你检查过这个链接了吗??
  • 伙计,如果您开始处理大量记录,那么在 url 上使用 unique 对您来说将非常昂贵。现在,关于您的问题,当您希望它成为index 时,您绝对必须在text 列上使用length。所以...例如$table->text('url', 1024)->change()
  • @KuldeepMishra 把我应该纠正的地方写出来,请不要写链接,因为我已经读过了,但可能我还不够了解

标签: mysql laravel migration


【解决方案1】:

试试DB Statement -

 public function up()
{
    DB::statement('ALTER TABLE stack_links DROP INDEX stack_links_url_index');
    DB::statement('ALTER TABLE stack_links MODIFY COLUMN url TEXT');
}
public function down()
{
    DB::statement('ALTER TABLE stack_links CREATE INDEX stack_links_url_index');
    DB::statement('ALTER TABLE stack_links MODIFY COLUMN url STRING');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 2021-02-04
    • 2013-01-08
    • 2016-10-09
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多