On Apr 29, 5:38 pm, Jeremy Evans <[email protected]> wrote:
> On Apr 29, 2:48 am, jeremyz <[email protected]> wrote:
>
> >   DB[:tjoins,:t1s].select(:tjoins.*).where
> > (:tjoins__t1_id=>:t1s__id).and(:t1s__a1=>'A').all
> > works fine, but using
> >   T1.eager_graph(:tjoins).where(:t1s__a1=>'A').select(:tjoins.*).all
> > generates a : undefined method `pk' for nil:NilClass
>
> > but the generated sql seems OK :
> > SELECT `tjoins`.* FROM `t1s` LEFT OUTER JOIN `tjoins` ON
> > (`tjoins`.`t1_id` = `t1s`.`id`) WHERE (`t1s`.`a1` = 'A')
>
> If you could post the full backtrace, I might be able to see what is
> going on.

The full backtrace is :

class T1 < Sequel::Model
    set_schema do
        primary_key :id, :auto_increment => true
        text :a1
    end
    one_to_many :tjoins
end
class Tjoin < Sequel::Model
    set_schema do
        foreign_key :t1_id, :table => :t1s, :null => false
        text :ajoin, :null => false
    end
    many_to_one :t1
end
T1.create_table!
Tjoin.create_table!
t1 = T1.create( :a1 => 'A' )
join1 = Tjoin.create( :t1_id=>t1.id, :ajoin=>'JOIN' )
puts T1.eager_graph(:tjoins).where(:t1s__a1=>'A').select
(:tjoins.*).all

[info] SELECT `tjoins`.* FROM `t1s` LEFT OUTER JOIN `tjoins` ON
(`tjoins`.`t1_id` = `t1s`.`id`) WHERE (`t1s`.`a1` = 'A')
/var/lib/gems/1.9.1/gems/sequel-2.12.0/lib/sequel/model/
associations.rb:1146:in `block in eager_graph_build_associations':
undefined method `pk' for nil:NilClass (NoMethodError)
        from /var/lib/gems/1.9.1/gems/sequel-2.12.0/lib/sequel/model/
associations.rb:1144:in `each'
        from /var/lib/gems/1.9.1/gems/sequel-2.12.0/lib/sequel/model/
associations.rb:1144:in `eager_graph_build_associations'
        from /var/lib/gems/1.9.1/gems/sequel-2.12.0/lib/sequel/model/
associations.rb:1294:in `post_load'
        from /var/lib/gems/1.9.1/gems/sequel-2.12.0/lib/sequel/dataset.rb:
125:in `all'
        from sequel_test.rb:31:in `<main>'

>
> One thing I should note is that the title of the post contains best
> practices.  In general, I wouldn't consider it a best practice to do
> what you are doing in terms of adding rows to the join table.  I would
> recommend using the join table as a real model table.  So instead of
>
>   t1.add_t2(t2, 'blah')
>
> do:
>
>   Tjoin.create(:t1=>t1, :t2=>t2, :ajoin=>'blah')
>
> Yes, it is more verbose, but you could do something like:
>
>   Tjoin.add(t1, t2, 'blah')
>
> with the appropriate definition of Tjoin.add.
>
> One reason I'm hesitant to have the default association methods like
> add_association take multiple arguments and pass them along is it
> would encourage people to do things like this, when I think it is
> better to use a real model table.
>
> Jeremy

Ok for the best practice, as I said I'm not an SQL expert.
If it's a feature which encourages a bad habbit, leave it.
I'm redefining my tests to see how it rolls using a real model for the
join table.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to