In the process of serving a view I am saving a record that an incoming
URL specified a tracking ID.
One of the URLs got mangled by somebody somewhere resulting in it
being decoded as a non-existent user ID.

# I would expect this to throw an integrity error
        tracking.save()


but it doesn't until TransactionMiddleware processes the outgoing
response.

At which point it throws an IntegrityError which Django does not
catch.

IntegrityError: insert or update on table "traffic_tracking2010"
violates foreign key constraint "traffic_tracking2010_user_id_fkey"
DETAIL: Key (user_id)=(1394967) is not present in table "auth_user".

Fortunately I have paste installed on top of Django so I got emailed
with the traceback (see below)

Can anybody figure out why the IntegrityError doesn't get thrown till
then ?
Why didn't Django catch errors thrown in middleware ?

I even tried this:

    sid = transaction.savepoint()
    try:
        tracking.save()
    except Exception,e:
        get_logger().exception("tracking save: %s (%s)" % (src,e))
        transaction.savepoint_rollback(sid)
    else:
        transaction.savepoint_commit(sid)

but again, no IntegrityError is thrown at that point.


Module paste.exceptions.errormiddleware:144 in __call__
<<              __traceback_supplement__ = Supplement, self, environ
                sr_checker = ResponseStartChecker(start_response)
                app_iter = self.application(environ, sr_checker)
                return self.make_catching_iter(app_iter, environ,
sr_checker)
            except:>>  app_iter = self.application(environ,
sr_checker)
Module django.core.handlers.wsgi:245 in __call__
<<                  # Apply response middleware
                    for middleware_method in
self._response_middleware:
                        response = middleware_method(request,
response)
                    response = self.apply_response_fixes(request,
response)
            finally:>>  response = middleware_method(request,
response)
Module django.middleware.transaction:25 in process_response
<<          if transaction.is_managed():
                if transaction.is_dirty():
                    transaction.commit()
                transaction.leave_transaction_management()
            return response>>  transaction.commit()
Module django.db.transaction:199 in commit
<<          using = DEFAULT_DB_ALIAS
        connection = connections[using]
        connection._commit()
        set_clean(using=using)
    >>  connection._commit()
Module django.db.backends:32 in _commit
<<      def _commit(self):
            if self.connection is not None:
                return self.connection.commit()

        def _rollback(self):>>  return self.connection.commit()
IntegrityError: insert or update on table "traffic_tracking2010"
violates foreign key constraint "traffic_tracking2010_user_id_fkey"
DETAIL: Key (user_id)=(1394967) is not present in table "auth_user".

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to