<p dir="auto"><code class="notranslate">@UniqueEntity</code> doesn't properly handle composite keys when one of them is a related entity. The database throws <code class="notranslate">Integrity constraint violation: 1062 Duplicate entry</code> when trying to persist an entity with the same company and name as an already existing one. Expected behavior is that <code class="notranslate">@UniqueEntity</code>'s validator blocks persisting the (duplicate) entity. Here's such an entity to illustrate the issue:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/**
 * @ORM\Entity
 * @ORM\Table(name=&quot;my_user&quot;,
 *  uniqueConstraints={@ORM\UniqueConstraint(columns={
 *      &quot;company_id&quot;, &quot;name&quot;
 *  })}
 * )
 * @UniqueEntity(fields={&quot;company&quot;, &quot;name&quot;})
 */
class MyUser {

    /**
     * @ORM\Column(name=&quot;id&quot;, type=&quot;integer&quot;, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy=&quot;AUTO&quot;)
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity=&quot;Company&quot;, inversedBy=&quot;myUsers&quot;)
     * @ORM\JoinColumn(name=&quot;company_id&quot;, referencedColumnName=&quot;id&quot;, nullable=false)
     * @Assert\NotNull
     */
    protected $company;

    /**
     * @ORM\Column(name=&quot;name&quot;, type=&quot;string&quot;, nullable=false)
     * @Assert\NotBlank
     */
    protected $name;

    // setters and getters omitted

}"><pre class="notranslate"><code class="notranslate">/**
 * @ORM\Entity
 * @ORM\Table(name="my_user",
 *  uniqueConstraints={@ORM\UniqueConstraint(columns={
 *      "company_id", "name"
 *  })}
 * )
 * @UniqueEntity(fields={"company", "name"})
 */
class MyUser {

    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Company", inversedBy="myUsers")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=false)
     * @Assert\NotNull
     */
    protected $company;

    /**
     * @ORM\Column(name="name", type="string", nullable=false)
     * @Assert\NotBlank
     */
    protected $name;

    // setters and getters omitted

}
</code></pre></div>
<table role="table">
<thead>
<tr>
<th>Q</th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<td>Bug report?</td>
<td>yes</td>
</tr>
<tr>
<td>Feature request?</td>
<td>no</td>
</tr>
<tr>
<td>BC Break report?</td>
<td>no</td>
</tr>
<tr>
<td>RFC?</td>
<td>no</td>
</tr>
<tr>
<td>Symfony version</td>
<td>4.0.4</td>
</tr>
</tbody>
</table>
<p dir="auto">Javascript error is report to the JS console (tested in Chrome and Edge) when the exception is handled by Symfony Profiler:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Uncaught TypeError: Cannot set property 'className' of null at Object.createTabs"><pre class="notranslate"><code class="notranslate">Uncaught TypeError: Cannot set property 'className' of null at Object.createTabs
</code></pre></div>
<p dir="auto">May be related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="259593839" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/24281" data-hovercard-type="pull_request" data-hovercard-url="/symfony/symfony/pull/24281/hovercard" href="https://github.com/symfony/symfony/pull/24281">#24281</a>.</p>
<h3 dir="auto">Steps to reproduce:</h3>
<ol dir="auto">
<li>Create a new flex project</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="composer create-project symfony/skeleton blog
cd blog
composer require server --dev
composer require profiler --dev "><pre class="notranslate"><code class="notranslate">composer create-project symfony/skeleton blog
cd blog
composer require server --dev
composer require profiler --dev 
</code></pre></div>
<ol start="2" dir="auto">
<li>Create a controller and enable its route in <code class="notranslate">config/routes.yaml</code>:</li>
</ol>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;?php
namespace App\Controller;

class DefaultController {
	public function index()
	{
		throw new \Exception('Wow!');
	}
}"><pre class="notranslate"><span class="pl-ent">&lt;?php</span>
<span class="pl-k">namespace</span> <span class="pl-v">App</span>\<span class="pl-v">Controller</span>;

<span class="pl-k">class</span> <span class="pl-v">DefaultController</span> {
	<span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">index</span>()
	{
		<span class="pl-k">throw</span> <span class="pl-k">new</span> \<span class="pl-v">Exception</span>(<span class="pl-s">'Wow!'</span>);
	}
}</pre></div>
<ol start="3" dir="auto">
<li>Run <code class="notranslate">php bin\console server:run</code></li>
<li>Open the browser</li>
<li>Info about the exception is rendered properly</li>
<li>But there is an error in the browser JS console</li>
</ol>