kusrabs
kusrabs

Reputation: 23

Unique constraints on foreign key in Doctrine

Is there any way to add constraints by foreign key in Doctrine? This is my config for an entity in Symfony 3.3. doctrine:scheme:validation command gives me an answer like "There is no column with name 'product' on table 'tariff'"

Rg\ApiBundle\Entity\Tariff:
    fields:
        price:
            type: float
            column: price
    manyToOne:
        product:
            targetEntity: Product
            inversedBy: tariffs
        timeunit:
            targetEntity: Timeunit
            inversedBy: tariffs
    uniqueConstraints:
        no_double_tariff_idx:
            columns:
                - product
                - timeunit

Upvotes: 2

Views: 2135

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52513

You need to reference the column's names (not the name of the relation used by doctrine). By default doctrine will suffix the relation's name with _id but you can configure the exact name of the join-column as shown in the following example configuration:

'Your\Entity\ProductVariant':
  manyToOne:
    image:
      targetEntity: 'Your\Entity\Product\Image'
      joinColumn:
        name: '`image_id`'
        referencedColumnname: 'id'
        nullable: false
        options:
          unique: false
    color:
      targetEntity: 'Your\Entity\Product\Color'
      joinColumn:
        name: '`color_id`'
        referencedColumnname: 'id'
        # [..]
  uniqueConstraints:
    only_one_image_of_same_product_color_idx:
      columns:
        - 'image_id'
        - 'color_id'

Upvotes: 1

Related Questions