Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

:limit + :offset incompatibility between SQLite and Postgres

#5316

SQLlite requires that a SQL statement with an OFFSET command include a LIMIT statement. The way to use an offset to return all remaining records with no limit, is to put a limit of -1. Postgres on the other hand disallows negative limits (since version 8.4), but allows the use of the OFFSET command without the LIMIT to achieve the same effect. (I believe MySQL also prevents negative limits.) That means the same activerecord statement, containing either a negative limit, or no limit, with an offset, won't work in both SQLite and Postgres.

Example:

This will work in Postgres, fail in SQLite:

scope :archives, :offset => 10

This will fail in Postgres, work in SQLite:

scope :archives, :offset => 10, :limit => -1

I'm not sure if this is something that activerecord can account for, or if it's just something developers need to be aware of (and change the code when switching databases). But since AR is supposed to be DB agnostic, it seems the point is that you should be able to swap out the DB without changing the code.

Reported by Sean · August 6th, 2010 @ 01:49 PM

State: resolved
Milestone: 3.0.2
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to open
    • Assigned user set to Santiago Pastorino
    • Tag set to activerecord
    • Importance changed from to Low

    Confirmed inconsistency in Rails 3.

    I'm guessing that for SQLite setting the :limit to -1 by default should make it work. And for Postgres overriding any negative limit settings to nothing should work.

    I could try to work on a patch in that direction, but is this desirable?

    P.S Santiago: I assigned to you since I don't know if anybody else is active in AR.

    October 7th, 2010 @ 03:33 AM

  2. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Assigned user changed from Santiago Pastorino to Aaron Patterson

    October 7th, 2010 @ 02:50 PM

  3. Jeremy Kemper
  4. Ryan Bigg
    Ryan Bigg
    • Tag changed from sheepskin boots, activerecord to activerecord

    Automatic cleanup of spam.

    October 16th, 2010 @ 02:38 AM

  5. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:36 AM

  6. Repository
    Repository
    • State changed from open to resolved

    (from [410114e85ac0048de3fd932a5aaac2a11b45be86]) adding a test to ensure offsets with no limits will work [#5316 state:resolved] http://github.com/rails/rails/commit/410114e85ac0048de3fd932a5aaac2...

    October 21st, 2010 @ 01:17 AM