skip to Main Content

I have an unusual index, which is consists of:
some_field, COALESCE(other_field, 0)
So I’m trying to use it in description of my entity


#[ORMUniqueConstraint(
    name: 'some_index',
    columns: ['some_field', 'COALESCE(other_field, 0)']
)]

Of course, doctrine cannot find such column and returns error, while running command php bin/console make:migration or php bin/console doctrine:migrations:diff:

There is no column with name "COALESCE(other_field, 0)" on table "..."

Is there any possible way to continue using commands from maker-bundle or doctine-bundle to generate new migrations without such kind of errors?

2

Answers


  1. Chosen as BEST ANSWER

    If somebody is interested in the idea of removing such kind of errors when executing command, I realized to add virtual property for doctrine's entity:

    #[ORMColumn(name: 'COALESCE(other_field, 0)', nullable: true, insertable: false, updatable: false)]
        private ?string $virtualField = null;
    

    Now it's generating extra fields(sql queries), but without any errors.


  2. Unfortunately, Doctrine doesn’t support complex SQL functions like COALESCE() directly in its schema management (e.g., when generating migrations or entity mapping), so it treats COALESCE(other_field, 0) as if it were a column name, hence the error.

    You can Manually Manage Custom Indexes in Migrations or else have to Ignore the Index in Doctrine Schema using

    php bin/console doctrine:schema:update --dump-sql
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search