# Column not in table (legacy database)

**URL:** <https://www.ruby-forum.com/t/column-not-in-table-legacy-database/160129>\
**Category:** Rails\
**Created:** [February 12, 2009, 12:22am UTC](https://www.ruby-forum.com/t/column-not-in-table-legacy-database/160129 "2009-02-12T00:22:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 12, 2009, 12:22am UTC](https://www.ruby-forum.com/t/column-not-in-table-legacy-database/160129/1 "2009-02-12T00:22:54Z")

</div>

I have been pushing my way through the issues regarding using legacy  
database tables and I am making some progress but have run into a  
couple of challenges.

The test application I am working with has an employee master file  
that has a primary key called “emssan”. I have an employee job record  
that has a primary key of “shssn”. There is one, and only one, job  
record for each employee record. I needed to join to the employee  
master file to get the name to display on the job record so I built  
this relationship:

class Job \< ActiveRecord::Base  
set\_primary\_key “shssn”  
def self.table\_name() “psts301” end  
belongs\_to :employee, :foreign\_key =\> “shssn”  
end

class Employee \< ActiveRecord::Base  
has\_one :job  
set\_primary\_key “emssan”  
def self.table\_name() “peis301” end  
def name  
“#{emfnam} #{emlnam}”  
end  
end

When I display the index, I get a list of employees with names.  
Great. When I take the option to show the record, I get a record  
returned to view. However, if I take the option to create or edit, I  
get an error  
Request Failed (code 500, Internal Error)  
The log shows the following:  
Column EMPLOYEE\_ID not in table PSTS301

The question is: Why is Rails looking for a key called EMPLOYEE\_ID  
when I set the primary key value to “emssan”? How do I fix this?

Thanks

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 12, 2009, 9:54am UTC](https://www.ruby-forum.com/t/column-not-in-table-legacy-database/160129/2 "2009-02-12T09:54:28Z")

</div>

On 12 Feb 2009, at 00:21, Pete wrote:

> class Employee \< ActiveRecord::Base  
> has\_one :job

has\_one :job, :foreign\_key =\> “shssn”, :dependent =\> :destroy

> set\_primary\_key “emssan”  
> def self.table\_name() “peis301” end  
> def name  
> “#{emfnam} #{emlnam}”  
> end  
> end
> 
> The question is: Why is Rails looking for a key called EMPLOYEE\_ID  
> when I set the primary key value to “emssan”? How do I fix this?

You probably need to define the foreign key in the has\_one  
relationship too.

Best regards

Peter De Berdt
