Hey Jeremy, thanks for your alternative; I'll try it shortly. For
completeness, here is the stacktrace + SQL log:

movies = Movie.join(:movies_labels, :movie_id => :id).limit(1).first
  (0.00) SELECT * FROM `movies` INNER JOIN `movies_labels` ON
(`movies_labels`.`movie_id` = `movies`.`id`) LIMIT

(works correctly)


movies = Movie.join(:movies_labels, :movie_id => :id).limit(1).count

(0.00) Mysql::Error: Duplicate column name 'updated_at': SELECT
COUNT(*) AS `count` FROM (SELECT * FROM `movies` INNER JOIN
`movies_labels` ON (`movies_labels`.`movie_id` = `movies`.`id`) LIMIT
1) AS `t1` LIMIT 1

Sequel::DatabaseError: Mysql::Error: Duplicate column name
'updated_at'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:200:in `query'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:200:in `_execute'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
database/logging.rb:32:in `log_yield'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:200:in `_execute'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:184:in `execute'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
connection_pool/threaded.rb:84:in `hold'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
database/connecting.rb:226:in `synchronize'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:184:in `execute'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
dataset/actions.rb:541:in `execute'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:440:in `execute'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
adapters/mysql.rb:366:in `fetch_rows'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
dataset/actions.rb:123:in `each'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
dataset/actions.rb:449:in `single_record'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
dataset/actions.rb:457:in `single_value'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
dataset/actions.rb:202:in `get'
        from /opt/local/lib/ruby/gems/1.8/gems/sequel-3.20.0/lib/sequel/
dataset/actions.rb:96:in `count'


On Apr 13, 12:20 pm, Jeremy Evans <[email protected]> wrote:
> On Apr 13, 12:00 pm, Phil Crosby <[email protected]> wrote:
>
>
>
>
>
>
>
>
>
> > Howdy,
>
> > I'm joining two tables together which share two columns (created_at,
> > updated_at). I'm joining them like this:
>
> > movies = Movie.join(:movies_labels, :movie_id => :id)
>
> > If I take the count of that dataset, "select *" is used, and since
> > created_at is unqualified and ambiguous, it creates an error. I can
> > work around this by qualifying the fields in the movies table
> > (Movie.qualify.join(...)) but I don't want to do this because a lot of
> > generic code (like pagination) depends on those movie fields being
> > unqualified.
>
> > Is there a way to qualify only the fields of the table being joined
> > on, or is there another approach I should try?
>
> First, a count should not use select *, it should use select count(*).
> Unless you reference the created_at column in some way, I'm not sure
> why you are getting an error.  You didn't provide the code, backtrace,
> or SQL log, and it's hard to diagnose without those things.
>
> Anyway, something like the following may work:
>
> movies = Movie.select(:movies.*).join(:movies_labels, :movie_id
> => :id).select_more(*{DB[:movies_labels].columns.map{|x|
> x.qualify(:movies_labels).as(:"movies_labels_#{x}")}})
>
> If you find yourself needing this more than once, it's not hard to
> code a generic dataset method that does the join+select_more.
>
> 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