janokary's avatar

Alter table change field to LONGTEXT;

Hello I tried to change

   Schema::table('table_name', function ($table) {
    $table->longText('column_name')->nullable()->change();
  });

but It didn't work so I changed it with tinker:

  DB::statement('ALTER TABLE tabel_name CHANGE  column_name column_name LONGTEXT;');

Is this a bug or something?

0 likes
12 replies
lindstrom's avatar

I'm actually working on some migrations so I could test this easily. I was able to change a string to longText using ->nullable()->change(). Did you require dbal?

composer require doctrine/dbal
janokary's avatar

@lindstrom. Actually what I was trying to do is to change a text field (not string) to longText.

lindstrom's avatar

Worked fine for me (L5.3 / MySQL5.7.10):

Requiring in composer.json

        "doctrine/dbal": "^2.5",

Test migration:

Schema::create('test_migrations', function (Blueprint $table) {
    $table->text('test');
});

Change:

Schema::table('test_migrations', function ($table) {
    $table->longText('test')->nullable()->change();
});

And the before and after: http://imgur.com/a/kVhRB

I think altering the table directly is fine and probably what most people would do. However, as you may know, there are some gotchas when you you want to do this on a table with a lot of data in production--mainly that it can take forever. See for example how one might speed it up: http://dba.stackexchange.com/questions/9746/mysql-fastest-way-to-alter-table-for-innodb

2 likes
lindstrom's avatar

NP. Not sure what's going with your set up. For fun, I also seeded with 100,000 records then changed just to ensure it was working with a decent amount of data. Other than taking a minute or two, it was fine. I didn't want to leave this thread without being a little more thorough and I always feel like an answer of "works for me" is pretty unsatisfying. Sorry.

1 like
mcmaxx's avatar
mcmaxx
Best Answer
Level 1

Ran into the same problem, a quick hack is to change it first to string, then to mediumText after (assuming it's originally in a text-ish format before).

You can do this in the same migration file and rollback the same way, works!

1 like
katjam's avatar

I added the DB statement to migration without the Schema wrapper:

public funciton up()
{
    DB::statement('ALTER TABLE tabel_name CHANGE  column_name column_name LONGTEXT;');
}

So no need to tinker on a production instance. Rollback works fine as:

 public function down()
{
    DB::statement('ALTER TABLE tabel_name CHANGE  column_name column_name TEXT;');
}

This only works for mysql databases. SQLite does not support column type changing (which means phpunit tests won't like it) and postgreSQL uses a different syntax.

You can use config('database.default') to determine the database type for each environment.

Please or to participate in this conversation.