Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validates_uniqueness_of should honor the collation #13465

Closed

Conversation

Projects
None yet
6 participants
@shugo
Copy link
Contributor

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?

Check the case sensitivity of the collation when the case_sensitive o…
…ption of validates_uniqueness_of is omitted.
def case_sensitive?
true
end

This comment has been minimized.

Copy link
@vipulnsward

vipulnsward Dec 24, 2013

Member

Is it case-sensitive by default for all adapters?

This comment has been minimized.

Copy link
@shugo

shugo Dec 24, 2013

Author Contributor

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

@dijonkitchen

This comment has been minimized.

Copy link
Contributor

commented Jan 21, 2017

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

@kamipo kamipo added the activerecord label Sep 3, 2017

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

Extract `default_uniqueness_comparison` to ease to handle mismatched …
…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 Feb 21, 2019

Deprecate mismatched collation comparison for uniquness validator
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 added a commit to kamipo/rails that referenced this pull request Mar 4, 2019

Deprecate mismatched collation comparison for uniquness validator
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

This comment has been minimized.

Copy link
Member

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
You can’t perform that action at this time.