Tuesday, May 15, 2007

Rails & MySQL gotcha

Hey everyone, just so you know. Don't ever do this in MySQL:

SELECT * FROM foo WHERE id IS NULL;

MySQL has a known bug that will cause this to return the last row inserted into the DB, ie. something random, instead of what you are looking for. This is fixed in MySQL version 5.0.25 which will probably be in our next upgrade. Until then:

do this:

def find_user_by_id(user_id)
  return nil if user_id == nil

  @user ||= User.find_by_id(user_id )
  @user
end

not this:

def find_user_by_id(user_id)
  @user ||= User.find_by_id(user_id )
  @user
end

For more information, see...

http://bugs.mysql.com/bug.php?id=14553

No comments: