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.

[Rails 2.3.4 and prior] validates_uniqueness_of fails with non STI inheritance

#3484

Hi,
I've found this issue when using class inheritance but non-STI

class User < ActiveRecord::Base
  validates_uniqueness_of :login
end

class Student < User
  set_table_name :students
end

Just created 1 column login for both tables

users(:login, :null => false)
students(:login, :null => false)
u = User.create(:login => 'toto' )
s = Student.create(:login => 'toto')
no such column: students.login: SELECT "users".id FROM "users" WHERE ("students"."login" = 'toto')  LIMIT 1

Reported by Hery · November 12th, 2009 @ 10:00 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Matt Jones
    Matt Jones

    What's the expected behavior in this instance? Should :login be unique across all records in the students table only, or across all records in users as well?

    The former would seem relatively straightforward (change the code that searches for a base class to exclude classes with a different table_name), while the latter would be extremely difficult.

    November 15th, 2009 @ 10:04 PM

  2. Hery
    Hery

    IMHO the expected behavior is a unique login in the students table only.

    I know it is a bad design since Mixin should be used instead of inheritance but I just pointed out that this simple code raises a MysqlError.

    November 15th, 2009 @ 10:18 PM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:36 AM