Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

validates_uniqueness_of does not take field length into account

#1042

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).

Reported by Arthur Klepchukov · September 14th, 2008 @ 10:20 AM

State: resolved
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Arthur Klepchukov
    Arthur Klepchukov

    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">]
    

    September 14th, 2008 @ 10:23 AM

  2. Arthur Klepchukov
    Arthur Klepchukov

    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]
    

    September 14th, 2008 @ 10:59 AM

  3. Arthur Klepchukov
    Arthur Klepchukov

    I went ahead and submitted a patch.

    September 14th, 2008 @ 11:16 AM

  4. Pratik
    Pratik
    • 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.

    December 20th, 2008 @ 06:11 PM

  5. Arthur Klepchukov
    Arthur Klepchukov
    • 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.

    December 24th, 2008 @ 04:47 AM

  6. Arthur Klepchukov
    Arthur Klepchukov
    • Tag changed from activerecord, bug, limit, migrations, validates_uniqueness_of, validation to activerecord, bug, limit, migrations, patch, validates_uniqueness_of, validation

    Tagged as patch.

    December 24th, 2008 @ 04:50 AM

  7. Pratik
    Pratik
    • State changed from incomplete to open

    December 24th, 2008 @ 05:01 AM

  8. Daniel Schierbeck
    Daniel Schierbeck

    Nice patch, with good, concise comments and a simple-to-follow test. +1.

    December 30th, 2008 @ 04:36 PM

  9. Repository
    Repository
    • 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...

    March 7th, 2009 @ 01:52 PM

  10. Jacob Burkhart
    Jacob Burkhart

    This needs to be fixed to support unicode, see #2653

    May 15th, 2009 @ 06:29 PM