Legacy database, primary key compound with two fields

I am trying to define a model for a legacy database and the primary key
is the combination of two fields, ‘branchcode’ and ‘debtorno’

class Custbranch < ActiveRecord::Base

set_table_name ‘custbranch’
set_primary_key ‘branchcode_branchno’

end

and I have tried…

set_primary_key ‘branchcode branchno’
set_primary_key ‘branchcode, branchno’

but in the console, a simple find, always an error…

@branch=Custbranch.find(“A365”)
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column
‘custbranch.branchcode_branchno’ in ‘where clause’: SELECT * FROM
custbranch WHERE (custbranch.branchcode_branchno = ‘A365’)

@branch = Custbranch.find(“A365”)
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column
‘custbranch.branchcode branchno’ in ‘where clause’: SELECT * FROM
custbranch WHERE (custbranch.branchcode branchno = ‘A365’)

@branch = Custbranch.find(“A365”)
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column
‘custbranch.branchcode, branchno’ in ‘where clause’: SELECT * FROM
custbranch WHERE (custbranch.branchcode, branchno = ‘A365’)

and the only way I seem to make it work is to use ‘branchcode’ as the
primary key which is not indexed. I can use this for now but I am
wondering if it is possible to set_primary_key on a legacy db model to
use a compound index/primary key.

Craig


This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Craig W. wrote:

I am trying to define a model for a legacy database and the primary key
is the combination of two fields, ‘branchcode’ and ‘debtorno’
[…]

If you can’t add a surrogate key, try the composite_primary_keys plugin.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs