Search code examples
laraveldoctrinedbal

Doctrine/dbal - Got error when try to change a string field to date


I created a table using a Laravel migration. I migrated two fields as strings but I want one as the date and one as an integer. So I created a new migration for changing those fields. I installed doctrine/dbal. I use Laravel 6.5. However, I got an error when trying to migrate.

Migration

public function up()
{
    Schema::table('follow_up_task', function (Blueprint $table) {
        $table->date('next_follow_date')->change();
        $table->integer('follow_stop_after')->change();
    });
}

public function down()
{
    Schema::table('follow_up_task', function (Blueprint $table) {
        $table->string('next_follow_date')->change();
        $table->string('follow_stop_after')->change();
    });
}

But I got an error.

Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci, CHANGE recurrin' at line 1 (SQL: ALTER TABLE recurring_tasks CHANGE next_recurring_date next_recurring_date DATE CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci, CHANGE recurring_stop_after recurring_stop_after INT CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci)

at /home/vagrant/laravel-api/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665|         // If an exception occurs when attempting to run a query, we'll format the error
666|         // message to include the bindings with SQL, which will make this exception a
667|         // lot more helpful to the developer instead of just the database's errors.
668|         catch (Exception $e) {
669|             throw new QueryException(
670|                 $query, $this->prepareBindings($bindings), $e
671|             );
672|         }
673| 

Exception trace:

1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci, CHANGE recurrin' at line 1") /home/vagrant/laravel-api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:63

2 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci, CHANGE recurrin' at line 1") /home/vagrant/laravel-api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:61

Please use the argument -v to see more details.


Solution

  • Then it could be because of bug in the most recent version of the doctrine/dbal v2.10.0 package.

    You can downgrade your doctrine/dbal package in composer.json to v2.9.3.

    https://github.com/laravel/framework/issues/30539#issuecomment-559605145

    You can try to use raw sql query in migration up() like this.

    DB::statement("ALTER TABLE .....");