# Exists? method for a table with no primary key

**URL:** <https://www.ruby-forum.com/t/exists-method-for-a-table-with-no-primary-key/168401>\
**Category:** Rails\
**Created:** [May 20, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/exists-method-for-a-table-with-no-primary-key/168401 "2009-05-20T12:25:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![manasi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7feea3/32.png) [@manasi](https://www.ruby-forum.com/u/manasi)\
**Post date:** [May 20, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/exists-method-for-a-table-with-no-primary-key/168401/1 "2009-05-20T12:25:01Z")

</div>

Hi,  
I am loving ruby on rails day by day… Today I wanted to use exists?  
method for Activerecord.

My model is -  
class PersonEngagement \< ActiveRecord::Base  
#this table does not have id as it is a legacy table  
#Also it has no primary key  
end

Attributes are person\_id, engagement\_id, etc

I want to check if a particular person \<\> engagement pair exists.

So I used PersonEngagement.exists? [‘person\_id = ?’,person.person\_id]  
and I got -  
ActiveRecord::StatementInvalid in ‘EngagementsHelper should check  
whether the pe  
rson works on an engagement’  
Mysql::Error: Unknown column ‘person\_engagements.id’ in ‘field list’:  
SELECT `pe rson_engagements`.id FROM `person_engagements` WHERE (person\_id = 123) A  
ND (`person_engagements`.project\_id = 1111) LIMIT 1

Should this be a patch to the original method ?  
P.S.  
Source of exists?  
# File vendor/rails/activerecord/lib/active\_record/base.rb, line  
688  
688: def exists?(id\_or\_conditions)  
689: connection.select\_all(  
690: construct\_finder\_sql(  
691: :select =\> “#{quoted\_table\_name}.#{primary\_key}”,  
692: :conditions =\> expand\_id\_conditions(id\_or\_conditions),  
693: :limit =\> 1  
694: ),  
695: “#{name} Exists”  
696: ).size \> 0  
697: end
