1

I have create a table stack_links as below

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']);
        });

Now i want to change url column to text type. So i have tried this

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

Hower it return this err

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

After searching, i found that the err because url is indexed(MySQL error: key specification without a key length). So i remove index but it still the same err. i follow : 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');
        });

Can anyone tell me where am i wrong.

Update : The problem is the parameter of dropIndex. When i replace it with the name of index. It work

Ngo Tuan
  • 205
  • 1
  • 2
  • 16
  • https://stackoverflow.com/questions/1827063/mysql-error-key-specification-without-a-key-length follow this i think this will help you. – Kuldeep Mishra Jan 09 '18 at 04:53
  • @KuldeepMishra i have removed index, but it still log the same err – Ngo Tuan Jan 09 '18 at 04:56
  • https://laravel.com/docs/5.5/migrations have you checked this link ?? – Kuldeep Mishra Jan 09 '18 at 05:05
  • Man, using `unique` on that `url` is going to be super costly for you in the future if you start dealing with tons of records. Now, regarding your problem, you absolutely must use a `length` on a `text` column when you want it to be an `index`. so... `$table->text('url', 1024)->change()` for instance. – Ohgodwhy Jan 09 '18 at 05:10
  • @KuldeepMishra just write out what i should correct , please don't write the link, because i have read it, but maybe i have not understand enough – Ngo Tuan Jan 09 '18 at 05:14
  • @Ohgodwhy: i know, but in this case, i don't want to limit the length of url, also remove all index on url. i want index on md5_url – Ngo Tuan Jan 09 '18 at 05:15
  • @NgoTuan try DB::statement() – Sohel0415 Jan 09 '18 at 06:02

1 Answers1

1

Try 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');
}
Sohel0415
  • 9,523
  • 21
  • 30