2013-01-15 41 views
3

我有一個Celery任務在我的Django應用程序中調用函數來處理CSV文件中的每一行,並嘗試使用模型將每行的信息插入到PostgreSQL數據庫中,如下所示:Django DatabaseError - 沒有這樣的保存點

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) 

我在循環的第一次迭代得到以下錯誤在Customer.objects.get_or_createDatabaseError: no such savepoint

然後我讓其後爲DatabaseError: current transaction is aborted, commands ignored until end of transaction block每次迭代。

我已經用腳本提供的相同信息成功插入(手動使用SQL)記錄到此表中。我也試圖與保存點工作,沒有運氣:

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) 

這是我的開發機器上發生的情況與家釀安裝的PostgreSQL 9.1。顯然,主要目標是解決這個問題,但任何有用的指針調試Celery任務或定位我的Postgres日誌文件也將有所幫助。

+0

嗨,你可以粘貼你的Customer.objects.get_or_create()方法嗎? – PepperoniPizza

+0

這只是內置的[Django方法](https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create) – Gady

回答

4

After doing some more research,它看起來像是在IntegrityError試圖強制保存點回滾,這不再存在的基本原因。我修復了IntegrityError,我假設沒有保存點問題已修復。仍然會很高興地看到,立即爲我調試的IntegrityError ...

啓用日誌記錄使用these instructions和我看到的是由get_or_create產生的INSERT語句違反了外鍵contraint。

+1

我編輯了你的答案,包括實際的答案;因爲這個問題不應該包含它內部的答案(如果答案是這樣的話,它不可能被稱爲'問題')。 –

+0

「這些說明」不再可訪問。 –

相關問題