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.

This project is archived and is in readonly mode.

update_all ignores conditions, when :orders and :limit options are supplied

#6058

Rails version: 3.0.2 and 3.0.3 (no such bug in Rails 3.0.1)
Ruby 1.8.7
Adapter: postgres

In code:

Reservation.update_all("status = 'ready'", 
  ["date = ? and crossing_point_id = ? and position > ?", 
   Time.zone.today, crossing_point.id, last_crossed],
  :order => :position, :limit => params[:count])

In log file:

AREL (15.0ms) UPDATE "reservations" SET status = 'ready' WHERE "reservations"."id" IN (SELECT "reservations"."id" FROM "reservations" ORDER BY position LIMIT 4)

After removing :order and :limit options

In code:

Reservation.update_all("status = 'ready'", 
  ["date = ? and crossing_point_id = ? and position > ?", 
   Time.zone.today, crossing_point.id, last_crossed])

In log file:

AREL (29.7ms)  UPDATE "reservations" SET status = 'ready' WHERE (date = '2010-11-24' and crossing_point_id = 2 and position > 182)

Reported by Valentine Bichkovsky · November 24th, 2010 @ 06:03 PM

State: open
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • State changed from new to open
    • Assigned user set to Neeraj Singh
    • Importance changed from to Low

    looking into it.

    November 24th, 2010 @ 06:56 PM

  2. Neeraj Singh
    Neeraj Singh

    Attached is a failing test.

    Relation is being built right. Look like issue is with Arel. Studying the code. Patch might take a while :-)

    November 24th, 2010 @ 07:24 PM

  3. rails
    rails
    • Tag changed from 3.0.2, arel, update_all to 302, arel, update_all

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 25th, 2011 @ 12:00 AM

  4. rails
    rails
    • State changed from open to stale

    February 25th, 2011 @ 12:00 AM

  5. Howard Yeh
    Howard Yeh
    • State changed from stale to open

    [state:open]

    this problem persists for 3.0.5.

    Ruby 1.9.2
    Adapter: postgres

    March 3rd, 2011 @ 10:26 AM

  6. Hugo Peixoto
    Hugo Peixoto

    Looks like the bug is in ARel indeed. When a subquery is generated, all other WHERE clauses are dropped.

    I attached a patch that fixes this bug and adds a test case.

    March 5th, 2011 @ 11:12 PM

  7. Hugo Peixoto
    Hugo Peixoto

    The previous patch kept the where conditions on the UPDATE statement. That is not the correct behavior, as the order/limit clauses wouldn't be applied with the WHERE clauses in mind.

    This one passes the WHERE clauses to the generated subquery.

    March 6th, 2011 @ 10:53 AM

  8. Santiago Pastorino
    Santiago Pastorino
    • Assigned user changed from Neeraj Singh to Aaron Patterson

    March 10th, 2011 @ 09:52 PM

  9. Aaron Patterson
    Aaron Patterson

    @Hugo I've applied this patch to ARel master which is the unreleased 2.1.0 version.

    Would you mind backporting this to 2.0.x?

    March 21st, 2011 @ 09:56 PM

  10. Hugo Peixoto