Skip to content

validates_uniqueness_of should honor the collation#13465

Closed
shugo wants to merge 1 commit intorails:masterfrom
shugo:uniqueness_case_sensitivity_default
Closed

validates_uniqueness_of should honor the collation#13465
shugo wants to merge 1 commit intorails:masterfrom
shugo:uniqueness_case_sensitivity_default

Conversation

@shugo
Copy link
Contributor

@shugo shugo commented Dec 24, 2013

Currently, if the case_sensitive option of validates_uniqueness_of is omitted, true is used by default.
However, the default collation of MySQL for rake db:create:all in Rails is utf8_unicode_ci, which means that you need to specify case_sensitive: false for each validates_uniqueness_of, except when the collation is explicitly set to *_bin. Otherwise, a DB-level unique constraint violation may occur even if the AR-level validation succeeded.

In MySQL each column has its own collation, and the preferred case sensitivity can be guessed from the value of the collation. Why not use the guessed value as the default?

…ption of validates_uniqueness_of is omitted.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it case-sensitive by default for all adapters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is. An adapter should override it, if necessary.

@dijonkitchen
Copy link
Contributor

Is this still relevant? Otherwise, it's stale and should be closed.

kamipo added a commit that referenced this pull request Feb 21, 2019
…collation issues

In MySQL, the default collation is case insensitive. Since the
uniqueness validator enforces case sensitive comparison by default, it
frequently causes mismatched collation issues (performance, weird
behavior, etc) to MySQL users.

https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/
#1399
#13465
gitlabhq/gitlabhq@c1dddf8
huginn/huginn#1330 (comment)

This extracts `default_uniqueness_comparison` to ease to handle the
mismatched collation issues on the connection.
kamipo added a commit to kamipo/rails that referenced this pull request Mar 4, 2019
In MySQL, the default collation is case insensitive. Since the
uniqueness validator enforces case sensitive comparison by default, it
frequently causes mismatched collation issues (performance, weird
behavior, etc) to MySQL users.

https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/
rails#1399
rails#13465
gitlabhq/gitlabhq@c1dddf8
huginn/huginn#1330 (comment)

I'd like to deprecate the implicit default enforcing since I frequently
experienced the problems in code reviews.

Note that this change has no effect to sqlite3, postgresql, and
oracle-enhanced adapters which are implemented as case sensitive by
default, only affect to mysql2 adapter (I can take a work if sqlserver
adapter will support Rails 6.0).
@kamipo
Copy link
Member

kamipo commented Apr 25, 2019

This will be fixed in Rails 6.1.

@kamipo kamipo closed this Apr 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants