我目前有一个使用 Heroku 的数据库,想将其迁移到 AWS,两者都使用 PostgresSQL。因此,在研究如何完成之后,我按照 this youtube video 中的步骤进行了操作。
我最初使用 Django 中的 Heroku 数据库凭据运行 python manage.py dumpdata > dumpdata.json
。
之后,我将 settings.py 中的数据库凭证更改为 AWS 数据库凭证,并运行成功运行的 python manage.py migrate --run-syncdb
。
然后我运行了代码 python manage.py loaddata dumpdata.json
,当我被抛出一个错误时。
出现以下错误:
django.db.utils.IntegrityError: Problem installing fixture 'C:\Users\Acer\Desktop\Web Development\Proffesional\eblossom\eblossom\eblossom\dumpdata.json': Could not load contenttypes.ContentType(pk=9): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq"
DETAIL: Key (app_label, model)=(user, profile) already exists.
我不明白这里出了什么问题,该站点一直运行良好,没有数据库编译错误,但现在当我尝试将其迁移到 AWS 时,我遇到了这个问题。
>以防万一我的models.py:
class Profile (models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mobile_number = models.CharField(max_length=12, null=True)
guest = models.BooleanField(default=False)
def __str__(self):
return f"{self.user}"
答案 0 :(得分:0)
我一直在数据库、本地和远程、postgres 或 sqlite 之间这样做。
为了实现这一点,请按顺序执行以下步骤:
# Start the same as you did on local database
python manage.py dumpdata > db.json
# push this file to your production/server location
# and delete or start with a fresh new database
python manage.py migrate
python manage.py shell
# Enter the following in the shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
# Exit shell and run following command
python manage.py loaddata db.json