Question

I've run into an odd problem when trying to order a dataset based on the results of a subquery. It works fine until I try to add a limit with an offset, then I get an error saying that the sort column name is invalid. Here's a debugger session that shows the issue (it's a bit of a silly query, I was just trying to simplify the real query as much as I could while still getting the error).

First I just create a simple select query, with a subquery

irb(main):009:0> ds = DB[:competency].select(:competencyname)                                                           
=> #<Sequel::TinyTDS::Dataset: "SELECT [COMPETENCYNAME] FROM [COMPETENCY]">
irb(main):010:0> ds = ds.select_more(Sequel.as Mssql::CompetencyRating.select(:rating).limit(1), :manager_rating )         => #<Sequel::TinyTDS::Dataset: "SELECT [COMPETENCYNAME], (SELECT TOP (1) [RATING] FROM [COMPETENCYRATING]) AS [MANAGER_RATING] FROM [COMPETENCY]">
irb(main):011:0> ds.count
=> 795

... everything looiks good so far, lets add a limit

irb(main):012:0> ds = ds.limit(10)
=> #<Sequel::TinyTDS::Dataset: "SELECT TOP (10) [COMPETENCYNAME], (SELECT TOP (1) [RATING] FROM [COMPETENCYRATING]) AS [MANAGER_RATING] FROM [COMPETENCY]">
irb(main):013:0> ds.count                                                                                                   => 10

Still A-OK, but if we change the limit to have an offset...

irb(main):015:0> ds = ds.limit(10,2)
=> #<Sequel::TinyTDS::Dataset: "SELECT TOP (10) [COMPETENCYNAME], [MANAGER_RATING] FROM (SELECT [COMPETENCYNAME], (SELECT TOP (1) [RATING] FROM [COMPETENCYRATING]) AS [MANAGER_RATING], ROW_NUMBER() OVER (ORDER BY [COMPETENCYNAME], [MANAGER_RATING]) AS [X_SEQUEL_ROW_NUMBER_X] FROM [COMPETENCY]) AS [T1] WHERE ([X_SEQUEL_ROW_NUMBER_X] > 2) ORDER BY [X_SEQUEL_ROW_NUMBER_X]">
irb(main):016:0> ds.count
Sequel::DatabaseError: TinyTds::Error: Invalid column name 'MANAGER_RATING'.
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/adapters/tinytds.rb:233:in `fields'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/adapters/tinytds.rb:233:in `block in fetch_rows'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/adapters/tinytds.rb:63:in `block in execute'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/database/connecting.rb:229:in `block in synchroni
ze'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/connection_pool/sharded_threaded.rb:128:in `hold'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/database/connecting.rb:229:in `synchronize'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/adapters/tinytds.rb:32:in `execute'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/dataset/actions.rb:793:in `execute'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/adapters/tinytds.rb:232:in `fetch_rows'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/dataset/actions.rb:143:in `each'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/dataset/actions.rb:583:in `single_record'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/dataset/actions.rb:591:in `single_value'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/dataset/actions.rb:250:in `get'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/sequel-4.3.0/lib/sequel/dataset/actions.rb:107:in `count'
        from (irb):16
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from /home/tlittle/.rvm/gems/ruby-1.9.3-p327@tms_mobile/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'irb(main):017:0> 

Seems like a bug to me, but I'd be thrilled to find out I'm just being stupid. Any idea what going wrong?

I'm using sequel 4.3.0 and tiny_tds 0.5.1.

Was it helpful?

Solution

This was answered on the Sequel-talk Google Group: https://groups.google.com/forum/#!topic/sequel-talk/FcGzky8qFiA

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top