覆盖 Doctrine 约束 UniqueConstraint

Override Doctrine constraint UniqueConstraint

我正在做一个使用 Sylius 的项目。我必须覆盖与 ChannelPricing 实体相关的默认约束。

这是原始约束声明: link转原文件:
https://github.com/Sylius/Sylius/blob/v1.10.7/src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ChannelPricing.orm.xml

    <mapped-superclass name="Sylius\Component\Core\Model\ChannelPricing" table="sylius_channel_pricing">
        <unique-constraints>
            <unique-constraint columns="product_variant_id,channel_code" name="product_variant_channel_idx" />
        </unique-constraints>
    </mapped-superclass>

当我在扩展实体 class:

上使用注释 @UniqueEntity 时,我能够使用我的新约束创建迁移
/**
 * @ORM\Entity
 * @ORM\Table(
 *     name="sylius_channel_pricing",
 *     uniqueConstraints={@UniqueConstraint(
 *     name="product_variant_channel_customer_group_idx",
 *     columns={"product_variant_id", "channel_code", "customerGroup_id"})},
 * )
 */
class ChannelPricing extends BaseChannelPricing

但是...这些行只生成迁移以创建约束 product_variant_channel_customer_group_idx.. 我想告诉 Doctrine 我想先删除默认约束 product_variant_channel_idx

我的错误是为约束定义了一个新名称。 如果我使用原始的,Doctrine 明白我想进行覆盖并生成 drop 指令。

因此,对于这些注释:

/**
 * @ORM\Entity
 * @ORM\Table(
 *     name="sylius_channel_pricing",
 *     uniqueConstraints={@UniqueConstraint(
 *     name="product_variant_channel_idx",
 *     columns={"product_variant_id", "channel_code", "customerGroup_id"})},
 * )
 */
class ChannelPricing extends BaseChannelPricing
{

生成的迁移:

public function up(Schema $schema): void
{
    // this up() migration is auto-generated, please modify it to your needs
    $this->addSql('DROP INDEX product_variant_channel_idx ON sylius_channel_pricing');
    $this->addSql('CREATE UNIQUE INDEX product_variant_channel_idx ON sylius_channel_pricing (product_variant_id, channel_code, customerGroup_id)');
}