Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Calling .last on model backed by legacy table attempts to order by "" (empty column name) but works with .first and .all #7149

Closed
garysweaver opened this Issue · 1 comment

2 participants

@garysweaver

Have a postgres table that looks like this in the DB (PostgreSQL 9.1.3):

    COLUMN_NAME            DATA_TYPE TYPE_NAME COLUMN_SIZE BUFFER_LENGTH DECIMAL_DIGITS NUM_PREC_RADIX NULLABLE REMARKS COLUMN_DEF SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION IS_NULLABLE SCOPE_CATLOG SCOPE_SCHEMA SCOPE_TABLE SOURCE_DATA_TYPE IS_AUTOINCREMENT 
    ---------------------- --------- --------- ----------- ------------- -------------- -------------- -------- ------- ---------- ------------- ---------------- ----------------- ---------------- ----------- ------------ ------------ ----------- ---------------- ---------------- 
    id                     -5         int8      19          (null)        0              10             0        (null)  (null)     (null)        (null)           19                1                NO          (null)       (null)       (null)      (null)           NO               
    field_1                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                2                NO          (null)       (null)       (null)      (null)           NO               
    field_2                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                3                NO          (null)       (null)       (null)      (null)           NO               
    field_3                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                4                NO          (null)       (null)       (null)      (null)           NO               
    field_4                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                5                NO          (null)       (null)       (null)      (null)           NO               
    field_5                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                6                NO          (null)       (null)       (null)      (null)           NO               
    field_6                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                7                NO          (null)       (null)       (null)      (null)           NO               
    field_7                 8         float8    17          (null)        17             10             0        (null)  (null)     (null)        (null)           17                8                NO          (null)       (null)       (null)      (null)           NO               
    field_8                 8         float8    17          (null)        17             10             0        (null)  0          (null)        (null)           17                9                NO          (null)       (null)       (null)      (null)           NO               
    field_9                 8         float8    17          (null)        17             10             0        (null)  0          (null)        (null)           17                10               NO          (null)       (null)       (null)      (null)           NO               
    field_10                8         float8    17          (null)        17             10             0        (null)  0          (null)        (null)           17                11               NO          (null)       (null)       (null)      (null)           NO               
    field_11                8         float8    17          (null)        17             10             0        (null)  0          (null)        (null)           17                12               NO          (null)       (null)       (null)      (null)           NO               
    field_12                8         float8    17          (null)        17             10             0        (null)  0          (null)        (null)           17                13               NO          (null)       (null)       (null)      (null)           NO               
    field_13                8         float8    17          (null)        17             10             0        (null)  0          (null)        (null)           17                14               NO          (null)       (null)       (null)      (null)           NO               

In config/schema.rb this specific table looks like:

  create_table "my_table", :id => false, :force => true do |t|
    t.integer "id", :limit => 8, :null => false
    t.float "field_1", :null => false
    t.float "field_2", :null => false
    t.float "field_3", :null => false
    t.float "field_4", :null => false
    t.float "field_5", :null => false
    t.float "field_6", :null => false
    t.float "field_7", :null => false
    t.float "field_8", :default => 0.0, :null => false
    t.float "field_9", :default => 0.0, :null => false
    t.float "field_10", :default => 0.0, :null => false
    t.float "field_11", :default => 0.0, :null => false
    t.float "field_12", :default => 0.0, :null => false
    t.float "field_13", :default => 0.0, :null => false
  end

There is data in all of the fields and the ids are sequential integers.

In Rails console, if I do:

  MyModel.first

it returns the first record. If I do:

  MyModel.all

It returns all records, including the last record. But, if I do:

  MyModel.last

It produces:

    MyModel Load (0.3ms)  SELECT "my_models".* FROM "my_models" ORDER BY "my_models"."" DESC LIMIT 1
    ActiveRecord::StatementInvalid: PG::Error: ERROR:  zero-length delimited identifier at or near """"
    LINE 1: ...M "my_models"  ORDER BY "my_models"."" DESC LI...
                                                                 ^
    : SELECT  "my_models".* FROM "my_models"  ORDER BY "my_models"."" DESC LIMIT 1
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1151:in `async_exec'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1151:in `exec_no_cache'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `block in exec_query'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `exec_query'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1246:in `select'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/querying.rb:38:in `block in find_by_sql'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/explain.rb:40:in `logging_query_plan'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/querying.rb:37:in `find_by_sql'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/relation.rb:171:in `exec_queries'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/relation.rb:160:in `block in to_a'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/explain.rb:33:in `logging_query_plan'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/relation.rb:159:in `to_a'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/relation/finder_methods.rb:390:in `find_last'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/relation/finder_methods.rb:146:in `last'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.6/lib/active_record/querying.rb:5:in `last'
        from (irb):16
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
        from /Users/my_user/.rvm/gems/ruby-1.9.3-p194@my_project/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'1.9.3p194
@carlosantoniodasilva

Apparently your table doesn't have a primary key, which is required for last (and other methods) to work. Please try creating a primary_key in your table, or setting a primary key using Active Record's self.primary_key= method, and let us know if you still have any problem with that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.