在Laravel迁移中将字符串更改为文本类型

时间:2018-01-09 04:52:04

标签: mysql laravel migration

我已经创建了一个表 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();

Hower它返回这个错误

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的参数。当我用索引的名称替换它。它的工作

1 个答案:

答案 0 :(得分: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');
}