This project is archived and is in readonly mode.

#615 ✓wontfix
flip

ActiveRecord exists? won't work with keyless tables

Reported by flip | July 13th, 2008 @ 11:46 PM | in 2.x

exists? needs a primary key to work. So it won't work with tables without a primary key.

  1. File activerecord/lib/active_record/base.rb, line 601

601: def exists?(id_or_conditions)

602: connection.select_all(

603: construct_finder_sql(

604: :select => "#{quoted_table_name}.#{primary_key}",

605: :conditions => expand_id_conditions(id_or_conditions),

606: :limit => 1

607: ),

608: "#{name} Exists"

609: ).size > 0

610: end

Comments and changes to this ticket

  • Tom Ward

    Tom Ward July 26th, 2008 @ 10:55 AM

    Can you provide a failing test case? The solution is pretty simple (select 1 rather than select primary_key), but in what circumstance would you actually have an ActiveRecord class without a primary key?

  • Pratik

    Pratik July 26th, 2008 @ 03:06 PM

    • State changed from “new” to “wontfix”

    I wonder what actually works when you don't have a PK. In any case, you should just write a plugin overriding exists?

  • fearless_fool

    fearless_fool October 26th, 2010 @ 07:06 PM

    • Importance changed from “” to “”

    Is there a good explanation for the wontfix status? Specifically, it is surprising that AR.exists?() fails on PK-less tables, given that AR.find() works just fine.

    > UserRole.find(:first, :conditions => {:user_id => 4, :role_id => 2}) => #

    Of course you can emulate AR.exists?() as:

    > !!UserRole.find(:first, :conditions => {:user_id => 4, :role_id => 2}) => true

    • ff

    P.S.:

    create_table "user_roles", :id => false, :force => true do |t|

    t.integer "user_id"
    t.integer "role_id"
    

    end

    and

    $ rake about About your application's environment
    Ruby version 1.9.2 (x86_64-darwin10.4.0)
    RubyGems version 1.3.7
    Rack version 1.2
    Rails version 3.0.0
    Active Record version 3.0.0
    Action Pack version 3.0.0
    Active Resource version 3.0.0
    Action Mailer version 3.0.0
    Active Support version 3.0.0
    Application root /Users/ff/Developer
    Environment development

  • fearless_fool

    fearless_fool October 26th, 2010 @ 07:11 PM

    • Tag changed from 2.1, activerecord, bug to 3.0, activerecord, bug

    Doh - once again with formatting...

      > UserRole.find(:first, :conditions => {:user_id => 4, :role_id => 2})
      => #<UserRole user_id: 4, role_id: 2>
    

    Of course you can emulate AR.exists?() as:

      > !!UserRole.find(:first, :conditions => {:user_id => 4, :role_id => 2})
      => true
    

    P.S.: Since this was originally a 2.1 ticket, please let me know if I should open a new ticket altogether for 3.0...

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>

Referenced by

Pages