On Thursday, February 14, 2013 5:03:36 AM UTC-8, Bert Spaan wrote:
>
> Hi all,
>
> I use Sequel to model two tables using a one_to_many association:
>
> class Node < Sequel::Model
> one_to_many :node_data
> end
>
> class NodeDatum < Sequel::Model
> many_to_one :node
> end
>
>
> And I get data from the DB using the following Sequel call:
>
> results = Node.where(:name=>name)
> .eager_graph(:node_data)
> .order(:id).all.map { |a|
> a.values.merge(:node_data=>a.node_data.map{|al| al.values}) }
>
>
> Also, next to the default columns from the nodes table, I need an extra
> column computed by a DB function:
>
> Node.select_append(Sequel.function(:collect_member_geometries, :members))
>
>
> However, I need the extra column and function call together with the
> eager_graph join, but when I try to do this, the extra column seems to
> get lost after the join.
>
> results = Node.where(:name=>name)
> .eager_graph(:node_data)
> .select_append(Sequel.function(:collect_member_geometries, :members))
> .order(:id).all.map { |a|
> a.values.merge(:node_data=>a.node_data.map{|al| al.values}) }
>
>
> Now the extra column (outcome of collect_member_geometries DB function)
> is not to be found in results.
>
> Does anyone know what I am doing wrong or what I should change to make
> this work?
>
>
You cannot use select_append when using eager_graph. You need to use
add_graph_aliases so that Sequel knows in which object to put the columns:
results = Node.where(:name=>name)
.eager_graph(:node_data)
.add_graph_aliases(:member_geometries=>[:nodes, :member_geometries,
Sequel.function(:collect_member_geometries, :members)])
.order(:id).all.map { |a| a.values.merge(:node_data=>a.node_data.map{|al|
al.values}) }
Also, just FYI, it's a good idea to always make sure that non-identifier
expressions in the SELECT clause have explicit aliases when using Sequel.
This is usually necessary when writing portable code (as different
databases vary in the default alias names they use), but even if you are
targetting a single database, if the default alias name chosen by the
database varies with the input data, it's possible to open yourself up to a
DoS attack (as Sequel converts the alias names returned by the database to
symbols). Note that the code you posted is not vulnerable to such an
issue, but it's still a good idea to alias the function call:
.select_append(Sequel.function(:collect_member_geometries,
:members).as(:some_alias))
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.