On Friday, October 12, 2018 at 9:07:05 AM UTC-7, Jeff Dyck wrote:
>
> I’m hoping someone can help me with a strange glitch I’ve run into…
>
>
> I've spent a bunch of time narrowing down the issue, and *think* the 
> problem is with a table which has a foreign key field named “model”.  
> Unfortunately 
> it's a legacy database so I can’t just change the field name to fix the 
> issue. 
>
>
> In my attempts to resolve the issue I have added a column alias of 
> :model_id pointing to :model, then specified :model_id as the association 
> key – that initially fixed all my issues, but now I've found instances 
> where that causes other problems which get fixed if I change the key back 
> to :model, which then breaks the original queries.
>
>
> While I'm feeling a bit stumped, I've narrowed the scope down to two 
> different errors which alternate depending on whether I use :model or 
> :model_id as the association key.
>
>
> I have the following tables and relationships defined (extraneous fields 
> removed for clarity).  Note, I have this database and models wrapped in a 
> module Inventory to keep it separate from another database my application 
> requires access to, hence the references to "Inventory::Device" and such.
>
>
> Device (tbl_equipment)
> ------------------------------- 
>  - id_equipment (key) 
>  - model (foreign key for models) 
>
> def_column_alias(:model_id, :model) 
>
> many_to_one :device_model, :key=>:model_id, :primary_key=>:id_model, :
> class=>'Inventory::Device_Model', :select=>[:id_model, :model_description, 
> :apple_model_number, :type] 
>
>
> Device_Model (tbl_model) 
> --------------------------------- 
> - id_model (key) 
>
> one_to_many :devices, :key => :model_id, :primary_key => :id_model, :class
> =>'Inventory::Device'  
>
>
>
You probably want:

many_to_one :device_model, :key=>:model_id, :key_column=>:model, 
:primary_key=>:id_model, :class=>'Inventory::Device_Model', 
:select=>[:id_model, :model_description, :apple_model_number, :type]

one_to_many :devices, :key => :model, :key_method=>:model_id, :primary_key 
=> :id_model, :class=>'Inventory::Device'  

See 
http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html#label-Associations+Based+on+SQL+Expressions+Options

You probably also want to use the column_conflicts 
plugin: 
http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/ColumnConflicts.html

If you still run into problems, please post back with the details.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to