首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖原则约束UniqueConstraint

覆盖原则约束UniqueConstraint
EN

Stack Overflow用户
提问于 2021-12-28 11:26:23
回答 1查看 262关注 0票数 1

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

下面是原始约束声明:指向原始文件的链接:

https://github.com/Sylius/Sylius/blob/v1.10.7/src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ChannelPricing.orm.xml

代码语言:javascript
复制
    <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>

当我在扩展实体类上使用注释@UniqueEntity时,我能够使用新的约束创建迁移:

代码语言:javascript
复制
/**
 * @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。我想告诉原则,我想先删除默认约束product_variant_channel_idx

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-29 19:51:59

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

因此,对于这些注释:

代码语言:javascript
复制
/**
 * @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
{

生成的迁移:

代码语言:javascript
复制
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)');
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70506369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档