[Bug][MySQL] Invalid query when creating a Json column with deafult value #7311

Open
opened 2026-01-22 15:49:42 +01:00 by admin · 0 comments
Owner

Originally created by @valentimarco on GitHub (Feb 6, 2024).

Bug Report

Q A
BC Break no
Version 2.15

Summary

I have a mysql@8.0.21 and i cannot set a JSON default value on one column.

Current behavior

From the entity below, i cannot setup correctly the default value of the column when is declare as json.
Further investigation discover that the orm does an invalid query to mysql:

  • correct query: CREATE TABLE setting (id INT AUTO_INCREMENT NOT NULL, settings JSON DEFAULT ('{}') NOT NULL, PRIMARY KEY(id))
  • ORM generated query: CREATE TABLE setting (id INT AUTO_INCREMENT NOT NULL, settings JSON DEFAULT '(''{}'')' NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB

mysql docs

How to reproduce

setting.php

#[ORM\Entity(repositoryClass: SettingRepository::class)]
class Setting
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    /**
     * @var array<string, mixed>
     */
    #[ORM\Column(type: 'json', options: ['default' => "('{}')"])]
    private array $settings = [];

   
    
    public function getId(): ?int
    {
        return $this->id;
    }
    

    /**
     * @return array<string, mixed>
     */
    public function getSettings(): array
    {
        return $this->settings;
    }

    /**
     * @param array<string, mixed> $settings
     */
    public function setSettings(array $settings): self
    {
        $this->settings = $settings;
        return $this;
    }
}

composer.json

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "repositories": [
    ],
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "ext-json": "*",
        "albocode/ccatphp-sdk": "0.2.*",
        "api-platform/core": "^3.2.0",
        "doctrine/annotations": "^2.0",
        "doctrine/doctrine-bundle": "^2.10",
        "doctrine/doctrine-migrations-bundle": "^3.2",
        "doctrine/orm": "^2.15",
        "knpuniversity/oauth2-client-bundle": "^v2.16.0",
        "nelmio/cors-bundle": "^2.3",
        "openswoole/core": "^22.1",
        "phpdocumentor/reflection-docblock": "^5.3",
        "phpstan/phpdoc-parser": "^1.22",
        "psr/log": "2.0.0",
        "sentry/sentry-symfony": "^4.9",
        "stevenmaguire/oauth2-keycloak": "^5.0",
        "symfony/asset": "7.0.*",
        "symfony/console": "7.0.*",
        "symfony/dom-crawler": "7.0.*",
        "symfony/dotenv": "7.0.*",
        "symfony/expression-language": "7.0.*",
        "symfony/flex": "^1.17|^2",
        "symfony/framework-bundle": "7.0.*",
        "symfony/mime": "7.0.*",
        "symfony/monolog-bundle": "^3.8",
        "symfony/process": "7.0.*",
        "symfony/property-access": "7.0.*",
        "symfony/property-info": "7.0.*",
        "symfony/runtime": "7.0.*",
        "symfony/security-bundle": "7.0.*",
        "symfony/serializer": "7.0.*",
        "symfony/string": "7.0.*",
        "symfony/translation": "7.0.*",
        "symfony/twig-bundle": "7.0.*",
        "symfony/uid": "7.0.*",
        "symfony/validator": "7.0.*",
        "symfony/yaml": "7.0.*",
        "twig/extra-bundle": "^3.8",
        "twig/string-extra": "^3.8"
    },
    "config": {
        "allow-plugins": {
            "composer/package-versions-deprecated": true,
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "7.0.*"
        }
    },
    "require-dev": {
        "dama/doctrine-test-bundle": "^v8.0",
        "deployer/deployer": "^7.3",
        "doctrine/doctrine-fixtures-bundle": "^3.4",
        "justinrainbow/json-schema": "^5.2",
        "phpstan/phpstan": "^1.10",
        "phpunit/phpunit": "^9.5",
        "swoole/ide-helper": "~5.0.0",
        "symfony/browser-kit": "7.0.*",
        "symfony/css-selector": "7.0.*",
        "symfony/debug-bundle": "7.0.*",
        "symfony/maker-bundle": "^1.50",
        "symfony/phpunit-bridge": "^7.0",
        "symfony/stopwatch": "7.0.*",
        "symfony/web-profiler-bundle": "7.0.*",
        "zenstruck/foundry": "^1.34"
    }
}

Pipeline to inizialize the database

        php bin/console doctrine:database:create --if-not-exists --env=${env}
	php bin/console doctrine:schema:drop --force --env=${env}
	php bin/console doctrine:schema:create --env=${env}
	php bin/console doctrine:fixtures:load --env=${env} -q

Expected behavior

I aspect to have the column of type json with the default json. I can resolve the problem by using migration but is not scalable...

Originally created by @valentimarco on GitHub (Feb 6, 2024). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.15 #### Summary I have a mysql@8.0.21 and i cannot set a JSON default value on one column. #### Current behavior From the entity below, i cannot setup correctly the default value of the column when is declare as json. Further investigation discover that the orm does an invalid query to mysql: - correct query: `CREATE TABLE setting (id INT AUTO_INCREMENT NOT NULL, settings JSON DEFAULT ('{}') NOT NULL, PRIMARY KEY(id))` - ORM generated query: `CREATE TABLE setting (id INT AUTO_INCREMENT NOT NULL, settings JSON DEFAULT '(''{}'')' NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB` [mysql docs](https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html) #### How to reproduce setting.php ```php #[ORM\Entity(repositoryClass: SettingRepository::class)] class Setting { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; /** * @var array<string, mixed> */ #[ORM\Column(type: 'json', options: ['default' => "('{}')"])] private array $settings = []; public function getId(): ?int { return $this->id; } /** * @return array<string, mixed> */ public function getSettings(): array { return $this->settings; } /** * @param array<string, mixed> $settings */ public function setSettings(array $settings): self { $this->settings = $settings; return $this; } } ``` composer.json ```json { "type": "project", "license": "proprietary", "minimum-stability": "stable", "prefer-stable": true, "repositories": [ ], "require": { "php": ">=8.1", "ext-ctype": "*", "ext-iconv": "*", "ext-json": "*", "albocode/ccatphp-sdk": "0.2.*", "api-platform/core": "^3.2.0", "doctrine/annotations": "^2.0", "doctrine/doctrine-bundle": "^2.10", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.15", "knpuniversity/oauth2-client-bundle": "^v2.16.0", "nelmio/cors-bundle": "^2.3", "openswoole/core": "^22.1", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpdoc-parser": "^1.22", "psr/log": "2.0.0", "sentry/sentry-symfony": "^4.9", "stevenmaguire/oauth2-keycloak": "^5.0", "symfony/asset": "7.0.*", "symfony/console": "7.0.*", "symfony/dom-crawler": "7.0.*", "symfony/dotenv": "7.0.*", "symfony/expression-language": "7.0.*", "symfony/flex": "^1.17|^2", "symfony/framework-bundle": "7.0.*", "symfony/mime": "7.0.*", "symfony/monolog-bundle": "^3.8", "symfony/process": "7.0.*", "symfony/property-access": "7.0.*", "symfony/property-info": "7.0.*", "symfony/runtime": "7.0.*", "symfony/security-bundle": "7.0.*", "symfony/serializer": "7.0.*", "symfony/string": "7.0.*", "symfony/translation": "7.0.*", "symfony/twig-bundle": "7.0.*", "symfony/uid": "7.0.*", "symfony/validator": "7.0.*", "symfony/yaml": "7.0.*", "twig/extra-bundle": "^3.8", "twig/string-extra": "^3.8" }, "config": { "allow-plugins": { "composer/package-versions-deprecated": true, "php-http/discovery": true, "symfony/flex": true, "symfony/runtime": true }, "optimize-autoloader": true, "preferred-install": { "*": "dist" }, "sort-packages": true }, "autoload": { "psr-4": { "App\\": "src/" } }, "autoload-dev": { "psr-4": { "App\\Tests\\": "tests/" } }, "replace": { "symfony/polyfill-ctype": "*", "symfony/polyfill-iconv": "*", "symfony/polyfill-php72": "*" }, "scripts": { "auto-scripts": { "cache:clear": "symfony-cmd", "assets:install %PUBLIC_DIR%": "symfony-cmd" }, "post-install-cmd": [ "@auto-scripts" ], "post-update-cmd": [ "@auto-scripts" ] }, "conflict": { "symfony/symfony": "*" }, "extra": { "symfony": { "allow-contrib": false, "require": "7.0.*" } }, "require-dev": { "dama/doctrine-test-bundle": "^v8.0", "deployer/deployer": "^7.3", "doctrine/doctrine-fixtures-bundle": "^3.4", "justinrainbow/json-schema": "^5.2", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^9.5", "swoole/ide-helper": "~5.0.0", "symfony/browser-kit": "7.0.*", "symfony/css-selector": "7.0.*", "symfony/debug-bundle": "7.0.*", "symfony/maker-bundle": "^1.50", "symfony/phpunit-bridge": "^7.0", "symfony/stopwatch": "7.0.*", "symfony/web-profiler-bundle": "7.0.*", "zenstruck/foundry": "^1.34" } } ``` Pipeline to inizialize the database ``` php bin/console doctrine:database:create --if-not-exists --env=${env} php bin/console doctrine:schema:drop --force --env=${env} php bin/console doctrine:schema:create --env=${env} php bin/console doctrine:fixtures:load --env=${env} -q ``` #### Expected behavior I aspect to have the column of type json with the default json. I can resolve the problem by using migration but is not scalable...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7311