This project is archived and is in readonly mode.
validates_uniqueness_of does not take field length into account
Reported by Arthur Klepchukov | September 14th, 2008 @ 10:20 AM | in 2.x
With the following Event model:
class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.string :title, :limit => 5
t.text :description
t.datetime :time
t.timestamps
end
end
def self.down
drop_table :events
end
end
class Event < ActiveRecord::Base
validates_uniqueness_of :title
end
I can do something along the lines of:
e = Event.create(:title => "abcde") => #e2 = Event.create(:title => "abcdefgh") => #Event.find_all_by_title("abcde") => [#, #]
The titles are unique until you apply the :limit => 5 but validates_uniqueness_of doesn't take this into account. This leaves two events with the title "abcde" in the database (MySQL, not SQLite, which doesn't enforce varchar limits).
Comments and changes to this ticket
-
Arthur Klepchukov September 14th, 2008 @ 10:23 AM
Sorry, the second part got mangled. Here it is again.
I can do something along the lines of:
>> e = Event.create(:title => "abcde") => #<Event id: 1, title: "abcde", description: nil, time: nil, created_at: "2008-09-14 09:01:14", updated_at: "2008-09-14 09:01:14"> >> e2 = Event.create(:title => "abcdefgh") => #<Event id: 2, title: "abcdefgh", description: nil, time: nil, created_at: "2008-09-14 09:01:31", updated_at: "2008-09-14 09:01:31"> >> Event.find_all_by_title("abcde") => [#<Event id: 1, title: "abcde", description: nil, time: nil, created_at: "2008-09-14 09:01:14", updated_at: "2008-09-14 09:01:14">, #<Event id: 2, title: "abcde", description: nil, time: nil, created_at: "2008-09-14 09:01:31", updated_at: "2008-09-14 09:01:31">]
-
Arthur Klepchukov September 14th, 2008 @ 10:59 AM
I realize that this could be taken care of if I set validates_length_of :title, :maximum => 5 in my model. However, I don't like having to repeat that (since I specified it in the migration) and I'm still worried about the database being in an inconsistent state.
A basic fix to this could be adding the following after line 669 in lib/active_record/validations.rb:
value = value[0, finder_class.columns_hash[attr_name.to_s].limit]
-
Pratik December 20th, 2008 @ 06:11 PM
- Assigned user set to Pratik
- State changed from new to incomplete
That's a good catch and it looks very tricky as well.
I can't see any patch. Could you please upload a patch here - http://rails.lighthouseapp.com/p... ?
Thanks.
-
Arthur Klepchukov December 24th, 2008 @ 04:47 AM
- Tag changed from activerecord, bug, migrations to activerecord, bug, limit, migrations, validates_uniqueness_of, validation
This patch makes ActiveRecord's validates_uniqueness_of take :limit into account for :string columns.
-
Arthur Klepchukov December 24th, 2008 @ 04:50 AM
- Tag changed from activerecord, bug, limit, migrations, validates_uniqueness_of, validation to activerecord, bug, limit, migrations, patch, validates_uniqueness_of, validation
Tagged as patch.
-
Pratik December 24th, 2008 @ 05:01 AM
- State changed from incomplete to open
-
Daniel Schierbeck December 30th, 2008 @ 04:36 PM
Nice patch, with good, concise comments and a simple-to-follow test. +1.
-
Repository March 7th, 2009 @ 01:52 PM
- State changed from open to resolved
(from [ccb0a92fa2ba0bf7be50033090c3a861e6d907be]) Ensure validates_uniqueness_of considers limit on the field. [#1042 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 1042 validates_uniqueness_of does not take field length into account (from [ccb0a92fa2ba0bf7be50033090c3a861e6d907be]) Ensure ...
- 2653 validates_uniqueness_of unicode aware field length The problem is in validates_uniqueness_of string concaten...