martin-georgiev / postgresql-for-doctrine

PostgreSQL enhancements for Doctrine. Provides support for advanced data types (json, jssnb, arrays), text search, array operators and jsonb specific functions.
https://packagist.org/packages/martin-georgiev/postgresql-for-doctrine
MIT License
364 stars 44 forks source link

jsonb type is interpreted as json in doctrine migrations #71

Open vbourdeix opened 4 years ago

vbourdeix commented 4 years ago

When creating an entity with a jsonb field and generating a migration with doctrine migrations, the migration defines the field as JSON, which forces me to fix that mannually each time I create a migration.

Do you have a way to fix this ?

martin-georgiev commented 4 years ago

Sounds like something is missing from the configuration. Can you share your setup, please?

vbourdeix commented 4 years ago

Here is my config/packages/doctrine.yaml :

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_pgsql'
        charset: utf8
        default_table_options:
            charset: utf8
            collate: en_US.UTF-8

        url: '%env(resolve:DATABASE_URL)%'
        types:
            jsonb: MartinGeorgiev\Doctrine\DBAL\Types\Jsonb
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

        dql:
            string_functions:
                ILIKE: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Ilike
                CONTAINS: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Contains
                JSON_GET_FIELD: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonGetField

And here is a typical field definition inside my entity :

/**
     * @ORM\Column(type="jsonb", nullable=true)
     */
    private $test = [];

Did I miss something ?

martin-georgiev commented 3 years ago

Did you find what's the problem?

Aweptimum commented 2 years ago

I tried reproducing this behavior while installing the package. Noticed that the mapping_types section is missing from @vbourdeix's doctrine config, so I omitted it and the migration does indeed default to json. Adding the following to doctrine.yaml fixes it

doctrine:
    dbal:
        mapping_types:
            jsonb: jsonb 
vbourdeix commented 2 years ago

Oh interesting !

That's pretty old so I can't remember how I handled it but I'll try to take a look again and try that soon.

Thanks 👍

Le mar. 22 mars 2022 à 15:41, Sam @.***> a écrit :

I tried reproducing this behavior while installing the package. Noticed that the mapping_types section is missing from @vbourdeix https://github.com/vbourdeix's doctrine config, so I omitted it and the migration does indeed default to json. Adding the following to doctrine.yaml fixes it

dbal:
    mapping_types:
        jsonb: jsonb

— Reply to this email directly, view it on GitHub https://github.com/martin-georgiev/postgresql-for-doctrine/issues/71#issuecomment-1075268620, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB4AGILDII6O6XJF2OCXNLVBHL2NANCNFSM4NZRRMVQ . You are receiving this because you were mentioned.Message ID: @.***>

adrianrudnik commented 7 months ago

Had something similar with JSONB columns always showing up in migrations, with a simple fix for posterity:

Don't do

 #[ORM\Column(type: 'json', options: ['jsonb' => true])]

do

#[ORM\Column(type: 'jsonb')]

Or the DBAL JSON type will always be compared to the JSON type here and generate a diff.