Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6778 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] find :conditions => 'id is null' returns a record with a non-null id

Reported by: timc Assigned to: bitsweat
Priority: normal Milestone: 1.2
Component: ActiveRecord Version: 1.1.6
Severity: normal Keywords: find mysql null
Cc:

Description

Aa is an empty class, subclassing ActiveRecord::Base. It has one column called 'nothing'

a = Aa.new

=> #<Aa:0xb723dacc @new_record=true, @attributes={"nothing"=>nil}>

a.save

=> true

Aa.find(:first, :conditions => 'id is null')

=> #<Aa:0xb7237050 @attributes={"id"=>"3", "nothing"=>nil}>

Aa.find(:first, :conditions => 'id is null')

=> nil

note the two find calls are identical, but the first call returns an unexpected result.

the log says:

SQL (0.000243) BEGIN SQL (0.000226) INSERT INTO aas (nothing) VALUES(NULL) SQL (0.003979) COMMIT Aa Load (0.001234) SELECT * FROM aas WHERE (id is null) LIMIT 1 Aa Load (0.000842) SELECT * FROM aas WHERE (id is null) LIMIT 1

version info: rails 1.1.6 mysql Ver 14.12 Distrib 5.0.26, for pc-linux-gnu (i686) using readline 5.1 mysql 2.7 gem Gentoo Linux

Attachments

fix_6778.patch (1.9 kB) - added by jonathan_viney on 12/07/06 02:03:48.

Change History

12/07/06 02:01:41 changed by jonathan_viney

  • summary changed from find :conditions => 'id is null' returns a record with a non-null id to [PATCH] find :conditions => 'id is null' returns a record with a non-null id.

I ran into this problem a while ago, here's a patch to fix it.

12/07/06 02:03:48 changed by jonathan_viney

  • attachment fix_6778.patch added.

12/07/06 02:08:36 changed by jonathan_viney

I'll just add that the variable that is set on the mysql connection is compatible with MySQL 3.23.5 and up, so it should be fairly safe.

http://dev.mysql.com/doc/refman/4.1/en/news-3-23-5.html

01/27/07 00:32:09 changed by bitsweat

  • keywords changed from find mysql to find mysql null.
  • owner changed from core to bitsweat.
  • milestone changed from 1.x to 1.2.

01/28/07 08:49:25 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [6064]) MySQL: SET SQL_AUTO_IS_NULL=0 so 'where id is null' doesn't select the last inserted id. Closes #6778.

01/28/07 08:51:35 changed by bitsweat

(In [6065]) Merge [6064] from trunk. References #6778.

01/28/07 09:11:16 changed by bitsweat

(In [6067]) Post title and body not null. References #6778.

01/28/07 09:12:04 changed by bitsweat

(In [6068]) Merge [6067] from trunk. References #6778.