Reputation: 4995
I have a function the a Celery task calls in my Django app that processes each row in a CSV file and tries to insert information from each row into the PostgreSQL database using a model, like so:
reader = csv.DictReader(csvfile, dialect=csv.get_dialect('excel'))
for row_number, row in enumerate(reader):
try:
...
customer, created = Customer.objects.get_or_create(
first_name = row['First Name'],
last_name = row['Last Name'],
email = row['Email'],
account_owner = account_owner
)
...
except Exception, e:
message = "%s: %s\n" % (type(e).__name__, e)
sys.stderr.write("%s\n" % message)
I'm getting the following error at Customer.objects.get_or_create
on the first iteration of the loop:
DatabaseError: no such savepoint
And then I'm getting DatabaseError: current transaction is aborted, commands ignored until end of transaction block
for each iteration thereafter.
I have successfully inserted (manually with SQL) a record into this table with the same information the script would provide. I have also tried working with savepoints, with no luck:
sid = transaction.savepoint()
customer, created = Customer.objects.get_or_create(
first_name = row['First Name'],
last_name = row['Last Name'],
email = row['Email'],
account_owner = account_owner
)
transaction.savepoint_commit(sid)
This is happening on my dev machine with PostgreSQL 9.1 installed from Homebrew. Obviously the main goal is to fix this issue, but any helpful pointers for debugging the Celery task or locating my Postgres log files would be helpful also.
Upvotes: 4
Views: 3061
Reputation: 4995
After doing some more research, it looks like the underlying cause of this is in an IntegrityError
which tries to force a savepoint rollback, which doesn't exist anymore. I fix the IntegrityError
and I assume the no savepoint issue is fixed. Still would be great to see the IntegrityError
immediately as I'm debugging...
Enabled logging using these instructions and I'm seeing that the insert statement generated by the get_or_create
violates a foreign key contraint.
Upvotes: 5