B
B
BonBon Slick2021-03-13 19:10:19
Doctrine ORM
BonBon Slick, 2021-03-13 19:10:19

Can't use unique-constraints on boolean or how to create partial index?

<entity name="App\Domain\UserPack\Email\Entity\UserEmail"
...
        <unique-constraints>
            <unique-constraint columns="user_uuid,isActive" name="unique_user_active_email"/>
            <unique-constraint columns="user_uuid,isFallback" name="unique_user_fallback_email"/>
        </unique-constraints>
...
        <embedded name="isFallback" class="App\Domain\UserPack\Email\ValueObject\UserEmailIsFallback"
                  use-column-prefix="false"/>
        <embedded name="isActive" class="App\Domain\UserPack\Email\ValueObject\UserEmailIsActive"
                  use-column-prefix="false"/>
...
        <many-to-one target-entity="App\Domain\UserPack\User\Entity\User"
                     field="user"
                     inversed-by="emails"
        >
            <cascade>
                <cascade-persist/>
            </cascade>
            <join-column name="user_uuid" referenced-column-name="uuid"/>
        </many-to-one>


If we replace boolean values ​​with others, say string
<unique-constraint columns="user_uuid,email" name="unique_user_active_email"/>

That seems to work, did not test requests.

SO 1
SO 2
Doctrine 2 docs

Why can't boolean values ​​be used for composite unique keys?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tukreb, 2021-03-14
@BonBonSlick

I don't have any problems creating unique-constraints for boolean.
https://www.postgresql.org/docs/current/indexes-pa...

* @ORM\Table(name="table",
 *     uniqueConstraints={
 *          @ORM\UniqueConstraint(name="only_one_true_primaryuix", columns={"id_table", "is_primary"}, options={"where":"is_primary"})
 *  },
 *     indexes={
 *          ///index
 *  })

I think my working example will push you to the right decision.
However. remember if doctrine has practically no restrictions for uniqueConstraints, then doctrine has problems with composite keys PRIMARY KEY ("id_1", "id_2"),and they work only in a very primitive form and it is strongly recommended to replace them with one primary key and use them uniqueConstraintsto emulate a composite key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question