Django (1.5) login exception (Save with update_fields did not affect any rows)

173 views
Skip to first unread message

AK

unread,
Mar 29, 2014, 7:23:29 PM3/29/14
to django...@googlegroups.com

In my Django web app (running on Google App Engine), when a new user signs up a User object is created and then that user is authenticated and logged in. Every once in a while, the login function throws an exception (DatabaseError: Save with update_fields did not affect any rows) and I'm not really sure why this is happening (this is my first Django app).

newuser = User.objects.create_user(username, "", password)

    newuser.first_name = username
    newuser.save() 


    # Create and save an associated accounts table to the system
    models.Accounts(user=newuser).save()


    # Authenticate the new user
    newuser = authenticate(username=username, password=password)

    # This is the login that occasionally throws an exception
    login(request, newuser)

jeroenp

unread,
Nov 17, 2014, 10:03:26 AM11/17/14
to django...@googlegroups.com
Hi,

the model.save() arguments `force_update` and `update_fields` cause an explicit check if the update statement matched any rows.

Every once in a while one of my applications runs into a race condition where one process does an update (with one of the flags) and another just removed an object. You then get a very generic DatabaseError with the message "Forced update did not affect any rows.", or "Save with update_fields did not affect any rows.". This invariably makes me scratch my head and wonder what happened.

As per ticket #21761, I don't understand why that is an error and a regular call to save() doesn't throw an error.

But since I can easily recover from this exception, I think it would be nice if these exceptions would be typed in a more specific subclass so that I can catch them and do something nice for the end-user.

As a side note, you might want to check that a single row is updated, and not "more than zero"; just in case a primary key is composed from more than one column and there's some other error...

kind regards
Jeroen Pulles
Reply all
Reply to author
Forward
0 new messages