Add possibility to use fields instead of column for unique constraint #6567

Closed
opened 2026-01-22 15:35:04 +01:00 by admin · 5 comments
Owner

Originally created by @alexander-schranz on GitHub (Nov 23, 2020).

Currently when using a unique constraint like:

<unique-constraints>
    <unique-constraint columns="locale,product_key" />
</unique-constraints>

you are forced to define the column name for the fields e.g.:

<field name="productKey" column="product_key" type="string" nullable="true" unique="true"/>

if you don't do this and somebody configure another naming_strategy for your entity it will not longer work as the column does then not exist.

It would be a great feature if you could use unique-constraints based on field name and doctrine orm will read the column name from the configured field e.g.:

<unique-constraints>
    <unique-constraint fields="locale,productKey" />
</unique-constraints>

<!-- no column is needed here now -->
<field name="productKey" type="string" nullable="true" unique="true"/>

What do you think about this?

Originally created by @alexander-schranz on GitHub (Nov 23, 2020). Currently when using a unique constraint like: ```xml <unique-constraints> <unique-constraint columns="locale,product_key" /> </unique-constraints> ``` you are forced to define the `column` name for the fields e.g.: ```xml <field name="productKey" column="product_key" type="string" nullable="true" unique="true"/> ``` if you don't do this and somebody configure another `naming_strategy` for your entity it will not longer work as the column does then not exist. It would be a great feature if you could use unique-constraints based on field name and doctrine orm will read the column name from the configured field e.g.: ```xml <unique-constraints> <unique-constraint fields="locale,productKey" /> </unique-constraints> <!-- no column is needed here now --> <field name="productKey" type="string" nullable="true" unique="true"/> ``` What do you think about this?
admin closed this issue 2026-01-22 15:35:04 +01:00
Author
Owner

@beberlei commented on GitHub (Dec 6, 2020):

This is a great idea, there are some caviats.

  • What about associations? Is every join column added? That would make sense.
  • Unique constraints need to be parsed after column/fields so that mapping info is available on CöassMetsdataInfo. This is not the case for all mapping drivers right now
@beberlei commented on GitHub (Dec 6, 2020): This is a great idea, there are some caviats. - What about associations? Is every join column added? That would make sense. - Unique constraints need to be parsed after column/fields so that mapping info is available on CöassMetsdataInfo. This is not the case for all mapping drivers right now
Author
Owner

@beberlei commented on GitHub (Feb 6, 2021):

The logic of resolving Unique Constraint (and Index as well) fields to columns would need to be done in SchemaTool, because access to NamingStrategy is needed.

@beberlei commented on GitHub (Feb 6, 2021): The logic of resolving Unique Constraint (and Index as well) fields to columns would need to be done in `SchemaTool`, because access to `NamingStrategy` is needed.
Author
Owner

@Lustmored commented on GitHub (Mar 4, 2021):

I took a shot at this and results are in #8523 - feedback is very welcome :)

@Lustmored commented on GitHub (Mar 4, 2021): I took a shot at this and results are in #8523 - feedback is very welcome :)
Author
Owner

@beberlei commented on GitHub (Apr 18, 2021):

Fixed in #8629

@beberlei commented on GitHub (Apr 18, 2021): Fixed in #8629
Author
Owner

@alexander-schranz commented on GitHub (Apr 18, 2021):

@beberlei @Lustmored This is really great to hear! Thank you for implementing this 🙏

@alexander-schranz commented on GitHub (Apr 18, 2021): @beberlei @Lustmored This is really great to hear! Thank you for implementing this 🙏
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6567