4

it is a way to have a field in an entity whith option nullable=true, unique=true ?

I try to do it, and I have an error when I update the database schema :

/**
 * @var string
 *
 * @ORM\Column(name="code", type="string", length=16, nullable=true, unique=true)
 */
private $code;

And the console :

php bin/console doctrine:schema:update --force
Updating database schema...

[Doctrine\DBAL\Exception\UniqueConstraintViolationException]                 
An exception occurred while executing 'CREATE UNIQUE INDEX 
UNIQ_6EEAA67DE76  
8E439 ON commande (code)':                                            
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 
'' fo  
r key 'UNIQ_6EEAA67DE768E439'
user7867717
  • 285
  • 3
  • 15
  • 1
    of course you have an error ... if several entries are null, then they have the same value, so they're not unique ... – t-n-y Jun 29 '17 at 08:40
  • Thats true; You tried to create several records with field = null and it is unique. – Albeis Jun 29 '17 at 08:43
  • 3
    This still depends on the DB, eg [MySQL](https://dev.mysql.com/doc/refman/5.7/en/create-table.html#create-table-indexes-keys) does allow this procedure. – Jenne Jun 29 '17 at 09:31
  • Ok thank's, I use MySQL. – user7867717 Jun 29 '17 at 09:50

0 Answers0