orm icon indicating copy to clipboard operation
orm copied to clipboard

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

Open valentimarco opened this issue 1 year ago • 0 comments

Bug Report

Q A
BC Break no
Version 2.15

Summary

I have a [email protected] 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...

valentimarco avatar Feb 06 '24 09:02 valentimarco