dbal: Can not change field definition to JSON with default value.
Bug Report
| Q | A |
|---|---|
| MySQL | 8.0.27 |
| DBAL | 3.4.4 |
| Laravel | 9.31.0 |
Summary
Can not change field definition to JSON with default value.
Current behaviour
In Laravel migration file, when trying to change field definition to JSON and set default value to (JSON_OBJECT()) generated SQL statement is wrong and can not be executed. Following is the MySQL error.
SQLSTATE[42000]: Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'json' can't have a default value (SQL: ALTER TABLE test CHANGE json json JSON DEFAULT '(JSON_OBJECT())' NOT NULL)
How to reproduce
Create migration file to change some column definition in Laravel. Replace up method of migration with this (this code assumes that you already have table called test with the field called json).
use Illuminate\Database\Query\Expression;
…
public function up(): void
{
Schema::table('test', function (Blueprint $table) {
$table->json('json')->default(new Expression('(JSON_OBJECT())'))->change();
});
}
Run migration and you will see the error.
Expected behaviour
As you can see in the generated error, default value of JSON field is surrounded with single quotes, which is the cause of the issue. If you will try to run following query (which is without quotes) then you will see that there would not be an error.
ALTER TABLE test CHANGE json json JSON DEFAULT (JSON_OBJECT()) NOT NULL
After a lot of time of debugging DBAL code I have found the cause of the issue.
File: src/Platforms/AbstractPlatform.php
Method: getDefaultValueDeclarationSQL
Right before the last return statement there should be added something like this (of course you will add some extra sanitization, because you better know how to do that).
if ($type instanceof Types\JsonType) {
return " DEFAULT $default";
}
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 15 (4 by maintainers)
Laravel in fact uses DBAL for the process, but it passes a
Illuminate\Database\Query\Expressionobject on togetAlterTableSQL()including the following column definition in theDoctrine\DBAL\Schema\TableDiffparameter:Now this seems odd, because DBAL does not support this type in
getDefaultValueDeclarationSQL(), so it will be cast as string, which in turn leads to this issue.The real issue here, as I see it, is that DBAL does not support any type to set a
JSON_ARRAY()orJSON_OBJECT()(or evenJSON_QUOTE(...)as a default value, so it’s just a (4 years old!) MySQL feature that is unsupported by DBAL. Laravel correctly sets the column type toDoctrine\DBAL\Types\JsonType, so a naïve working fix inPlatforms/AbstractMySQLPlatformwould be to add something like…You may have a more sophisticated way to do this in mind. However, the above would fix the issue while keeping it restricted to those particular functions and not breaking null or string (on MariaDB) defaults.
What do you think? I’ll be happy to provide a PR for this.
I would probably just switch to L11 to be honest, which doesn’t use DBAL anymore at all.