0

I need to change a Blob field type to a Varchar(128). The data in the field will fit the target field size, it's text, and shouldn't have a problem with UTF-8.

Sample data, all data is in this format: {"weight":"0","height":"0","width":"0","length":"0"}

I'm using Laravel Make:migrate to handle the conversion.

How should I write the SQL?

I know how to write a Laravel Migration. I also know how to alter a field. But a Blob isn't a text field nor is a Blob normally converted down to a Varchar. I've done some manual UTF-8 conversion of Blobs in the past and know you can mess up your data if you don't do it right. So my concern here is to not mess up my data with a Laravel Migrate. I don't believe the migrate 'down' method can undo corrupted data.

If my data fits the varchar target size and if the data fits the UTF-8 charset, am I good with a straight forward Alter statement:

DB::query("ALTER TABLE DB1.products CHANGE COLUMN dimensions dimensions varchar(128) DEFAULT NULL;");

Scotty
  • 130
  • 13
  • Possible duplicate of [Laravel migration table field's type change](https://stackoverflow.com/questions/32940495/laravel-migration-table-fields-type-change) – Sagar Gautam Jan 08 '19 at 14:44
  • My concern is not how to alter with a Laravel migration. I know how. I also know how to alter a field. It's just Blobs are not normally altered to Varchars. I not finding any returns in Google on converting a Blob to a Varchar. I don't want to crap my data with a migration because I missed something I should have considered. – Scotty Jan 08 '19 at 14:51
  • @Scotty updated my answer – Volod Jan 08 '19 at 15:02

3 Answers3

1

You shouldn't use sql for this, just create a migration and use change method

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

https://laravel.com/docs/5.7/migrations#modifying-columns

Considering your comment sql would be

ALTER TABLE tablename MODIFY column_name VARCHAR(128);
Volod
  • 1,283
  • 2
  • 15
  • 34
  • The change() method requires the doctrine/dbal dependency. I don't have that installed. The 'ALTER' statement didn't work. – Scotty Jan 08 '19 at 15:33
0

Run composer install and then composer update in the console and

drop your table from the database and also delete the migration ,then create a new migration using

php artisan make:migration change_col_datatype --table=table_name

and then make changes as below

Schema::table('your_table_name', function ($table) {
    $table->string('your_table_name');
});

public function down()
    {
        Schema::dropIfExists('tablename');
    }
piet.t
  • 11,718
  • 21
  • 43
  • 52
Prathamesh Doke
  • 797
  • 2
  • 16
  • 38
-1

The SQL statment:

\DB::statement('ALTER TABLE products CHANGE COLUMN dimensions dimensions VARCHAR(128) DEFAULT NULL;');

Worked fine.

Scotty
  • 130
  • 13