I just noticed this hasn't gotten a reply. You might have moved on from this issue, but for what it's worth: Since it works manually and when the snippet of the code is run, it's puzzling why it would not work in your other instance. One guess is that it could be a DB corruption. Try importing the DB into a new, blank DB and try it again on that.