Rails custom foreign key not respected -
the relevant database tables have these schemas:
sqlite> .schema structures create table structures( struct_id integer primary key autoincrement not null, batch_id integer, tag text, input_tag text, foreign key (batch_id) references batches(batch_id) deferrable deferred); sqlite> .schema residues create table residues( struct_id integer not null, resnum integer not null, name3 text not null, res_type text not null, foreign key (struct_id) references structures(struct_id) deferrable deferred, primary key (struct_id, resnum));
i have following models:
class structure < activerecord::base set_table_name "structures" self.primary_key = "struct_id" attr_accessible :struct_id, :batch_id, :input_tag has_many :residues end class residue < activerecord::base self.primary_keys :struct_id, :resnum belongs_to :structure, :foreign_key => 'struct_id' attr_accessible :name3, :res_type, :resnum end
in structures show have:
<h2>residues</h2> <% @structure.residues.each |residue| %> <p> <b>residue number:</b> <%= residue.resnum %> </p> <p> <b>residue type:</b> <%= residue.res_type %> </p> <% end %>
however, when try show structure following error:
sqlite3::sqlexception: no such column: residues.structure_id
why structure_id being looked in database , not struct_id? seems foreign key not being respected.
you need specify foreign key on both sides of relationship ( has_many , belongs_to)
Comments
Post a Comment