I experienced something similar where we had a column named :model in one
of our tables, and whenever I tried to call something like
Model.first.model, it returned Model (the class). In our case it wasn't a
foreign key, so we could get around it by creating an alias that did
instance.values[:model]. Not sure how this works out when eager/eager_graph
comes into the picture, but I'm guessing that while calling the
association, Sequel is trying to call device_instance.model to get the ID
to match up with the eager-loaded device_model_instance, and is receiving
Device instead of an id. I am curious to see what the actual problem is and
what the best solution for this problem is, hopefully Jeremy or someone
else can help better than I.

On Fri, Oct 12, 2018 at 11:07 AM Jeff Dyck <fsjj...@gmail.com> 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'
>
>
> *First error: Eager graph not working with :model_id as key*
>
>
> When I define the association using the :model_id column_alias, and do a
> search like:
>
>
> Inventory::Device.select(:serial_number, :nom_ordinateur).appletvs.
> eager_graph(:site, :device_model).order{site[:school_short_name]}.all
>
>
> I get the following error:
>
>
> /Library/Ruby/Gems/2.3.0/gems/sequel-5.12.0/lib/sequel/adapters/tinytds.rb
> :205:in `fields': TinyTds::Error: Invalid column name 'MODEL_ID'.
> (Sequel::DatabaseError)
>
>
>
> By playing around I've managed to narrow the issue down to eager_graphing
> the :device_model.  If I remove that part of the query it works.  It also
> works fine if I change the association to use :model as the key.
>
>
> *Second error: Can't express Device as a SQL literal error with :model as
> key*
>
> Once I switch the key back to :model, if I query and get a Device object,
> but then try to access the device_model accessor, I get an error that the
> Device cannot be expressed as a SQL literal, for example, if I query a
> device like below:
>
>
> results = Inventory::Device.where(:serial_number => "serial").eager(:user,
> :site, :device_model).first
>
> And then try to access results.device_model I get the error.  I get the
> same thing if I remove the eager option.  If I switch the association key
> back to :model_id then this works but the first error kicks in again.
>
>
> --
> 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.
>

-- 
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