使用子查询订购Sequel数据集并使用偏移量进行限制

时间:2013-12-18 20:10:28

标签: ruby sequel tiny-tds

在尝试根据子查询的结果排序数据集时,我遇到了一个奇怪的问题。它工作正常,直到我尝试添加带偏移的限制,然后我得到一个错误,说排序列名称无效。这是一个显示问题的调试器会话(这是一个有点愚蠢的查询,我只是想尽可能地简化真正的查询,同时仍然得到错误)。

首先,我只使用子查询

创建一个简单的选择查询
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

...到目前为止,所有东西都很好,让我们添加一个限制

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

还是A-OK,但是如果我们改变限制以获得偏移......

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> 

对我来说似乎是一个错误,但我很高兴发现我只是愚蠢。知道出了什么问题吗?

我正在使用续集4.3.0和tiny_tds 0.5.1。

1 个答案:

答案 0 :(得分:1)