Answer the question
In order to leave comments, you need to log in
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
Is there a default for a string field?
Change it too
$table->integer('year')->nullable()->default(null)->change();
Film::whereYear('Не указано')->update(['year' => null])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question