BernardA
BernardA

Reputation: 1523

Symfony Doctrine UniqueConstraint error Constant expression contains invalid operations

Attempting to create a UniqueConstraint for two fields with Doctrine on a Postgres db, I am getting the error when running make:migration.

"Compile Error: Constant expression contains invalid operations"

I searched around on SO and it seems to be related to creating a static property in a class.

Not sure why it is interpreting the code below that way:

 #[
ORM\Entity(repositoryClass: SalesChannelRepository::class),
ORM\Table(
    name: "sales_channel",
    schema: "my_schema",
    UniqueConstraint: [
        UniqueConstraint(
            name: "name_idx",
            fields: ["sales_channel_name_id", "multi_channel_id"]
        ),
    ]
),
]

The attributes seem in line with Doctrine documentation.

I did try a few variants, like using columns instead of fields, or using ORM\UniqueConstraint, but that did not help.

Running make:migration after removing that specific attibute works fine.

Upvotes: 2

Views: 739

Answers (1)

Monnomcjo
Monnomcjo

Reputation: 725

According to the documentation (https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/attributes-reference.html#attrref_uniqueconstraint), and what I practice. You should try to fix with:

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SalesChannelRepository::class)]
#[ORM\Table(name: "sales_channel")]
#[ORM\UniqueConstraint(name: "name_idx", columns: ["sales_channel_name_id", "multi_channel_id"])]
class SalesChannel
{...

Upvotes: 2

Related Questions