Jakoss
Jakoss

Reputation: 5255

Doctrine ORM unique string is not case sensitive

I got an Entity with field like that:

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

But if i try to fix a typo in name from "test" to "Test" i got an error that this name is already taken! Any idea why is that happening? Is an unique index case insenstive?

Upvotes: 4

Views: 3832

Answers (1)

hidden_4003
hidden_4003

Reputation: 1799

Your problem is because you have utf8_general_ci collation note the _ci which means case-insensitive, in turn that means that database treats "Test", "test", "TEst" etc. identically.

You will have to change column collation in the database manually to utf8_bin, or to utf8_xxx_cs (where xxx is language name) if you happen to have one for the needed language.

From Doctrine ORM FAQ:

4.1.1. How do I set the charset and collation for MySQL tables?

You can’t set these values inside the annotations, yml or xml mapping files. To make a database work with the default charset and collation you should configure MySQL to use it as default charset, or create the database with charset and collation details. This way they get inherited to all newly created database tables and columns.

Upvotes: 8

Related Questions