A
A
arms16312022-04-17 11:30:29
PHP
arms1631, 2022-04-17 11:30:29

How to change the data type of a database column in Laravel?

There is a column "YEAR" in the DB table. It has the data type "STRING". I need to change the data type to "INTEGER".
I installed doctrine/dbal. I write the migration:
php artisan make:migration alter_table_films_change_year --table=films

In the migration class itself:

public function up()
{
Schema::table('films', function (Blueprint $table) {
$table->integer('year' )->change();
});
}

/**
* Reverse the migrations.
*
*/
public function down()
{
Schema::table('films', function (Blueprint $table) {
$table->
});
}

But it gives an error:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 (SQL: ALTER TABLE films CHANGE year year INT DEFAULT Not specified)

The BL table already has data in the "YEAR" column as rows like "2020" and so on, perhaps because of this trouble, although it is unlikely
Help to understand the issue, until I found a solution

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2022-04-17
@delphinpro

Is there a default for a string field?
Change it too

$table->integer('year')->nullable()->default(null)->change();

Well, you may have to go through the table with your hands and remove the "Not Specified" values ​​​​from this field, since it cannot be converted to an integer type.
Can be requested, directly in migration
Film::whereYear('Не указано')->update(['year' => null])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question