Forum: Ruby on Rails Column not in table (legacy database)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1f77113371c7d36eae5c8a1f10137438?d=identicon&s=25 Pete (Guest)
on 2009-02-12 00:22
(Received via mailing list)
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
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2009-02-12 09:54
(Received via mailing list)
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
This topic is locked and can not be replied to.