api-platform / api-platform/core

Entity with two unique fields cannot be PUT (duplicate entry exception)

Open
#8,071 3 comments 0 reactions 0 assignees View on GitHub
doctrine Serializer State
Dominant language
PHP
Stars
2.6k
Forks
980
Avg merge
2d 5h
Merged PRs (30d)
48

Description

**API Platform version(s) affected**: 3.1.1

**Description**
An Entity with two combined properties being unique together cannot be PUT, as a child inside the parent Entity. When trying to PUT the parent Entity (`Customer`) with the child Entity (`CustomerTask`) that has the 2 unique fields (`$task` + `$customer`), the API tells that the Entity `CustomerTask` does already exist, though should actually replace it.

Error Message:
> An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '\\x01\\x86\\x7F~X?r\\xA8\\xA4\\xC' for key 'unique_customer_task'

**How to reproduce**
This is how the entity looks:
```php
#[ORM\Entity(repositoryClass: CustomerTaskRepository::class)]
#[ORM\UniqueConstraint(name: 'unique_customer_task', fields: ['task', 'customer'])]
#[UniqueEntity(fields: ['task', 'customer'], message: 'Task already exist for customer')]
class CustomerTask
{
#[ORM\Id]
#[ORM\Column(type: "uuid", unique: true)]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: "doctrine.uuid_generator")]
private ?Uuid $id = null;

// Affected Unique Properties:
#[ORM\ManyToOne(inversedBy: 'customerTasks')]
#[ORM\JoinColumn(nullable: false)]
private ?Task $task = null;

#[ORM\ManyToOne(inversedBy: 'customerTasks')]
#[ORM\JoinColumn(nullable: false)]
private ?Customer $customer = null;

/....
}
```

**Possible Solution**

**Additional Context**
Is there a known issue with multiple unique fields? Or am I doing sth wrong?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.