2012-08-09 25 views
0

我收到以下錯誤時:IntegrityError使用Django的loaddata切換到Postgres的

IntegrityError: duplicate key value violates unique constraint "users_userprofile_pkey" 

我從MySQL遷移到Postgres的,所以我用傾銷從MySQL數據庫中的數據:

python2.7 manage.py dumpdata --indent=4 --natural > dump.json 

我得到的錯誤,當我試圖將dump.json加載到PostgreSQL數據庫:

python manage.py loaddata dump.json 

我有以下的信號,我的用戶/型號:

post_save.connect(create_user_profile, sender=User, dispatch_uid="user_create_profile") 
post_save.connect(create_api_key, sender=User, dispatch_uid="user_create_api_key") 

回答

2

我不得不註釋掉post_save信號,然後做loaddata。

0

問題可能是因爲--natural關鍵字,如果你瞭解dumpdata自然鍵here你會看到它的文檔具有適用於一些問題你數據庫遷移。

另外here他們談論這個問題的解決方案似乎很有趣(和單調乏味)。

您可以隨時嘗試添加不相互依賴的模型,以便在嘗試創建另一個模型之前確保它們存在。即:

如果你有這樣的模型:

class Person(models.Model): 
    first_name = models.CharField(max_length=100) 
    last_name = models.CharField(max_length=100) 

    birthdate = models.DateField() 

    class Meta: 
     unique_together = (('first_name', 'last_name'),) 

class Book(models.Model): 
    name = models.CharField(max_length=100) 
    author = models.ForeignKey(Person) 

然後遷移Person類,然後再書類。

此外,如果你可以張貼的錯誤,將是非常有益的(因爲我能更具體),但我很肯定的問題是一個主要的關鍵問題

+0

我有一個UserProfile依賴於用戶模型。我如何只轉儲用戶數據(這是用Django內置的)。另外,當兩個模型相互依賴時,順序應該是什麼? – egidra 2012-08-09 22:57:51

+0

爲用戶你可以'./ manage.py dumpdata auth'之後,只需轉儲您的UserProfile。對於相互依賴的模型,您應該使用我提到的乏味方法。遷移數據庫並非易事。這裏是另一個可能會幫助你的問題http://stackoverflow.com/questions/4964615/how-can-i-easily-convert-a-django-app-from-mysql-to-postgresql – Hassek 2012-08-09 23:12:18