Django: What are the best practices to migrate a project from sqlite to PostgreSQL

后端 未结 7 1190
礼貌的吻别
礼貌的吻别 2020-12-04 10:25

I need to migrate a complex project from sqlite to PostgreSQL. A lot of people seems to have problem with foreign keys, data truncature and so on...

  • Is there a
相关标签:
7条回答
  • 2020-12-04 11:15

    The following is a refinement of Nimo's answer and Stephen's answer for Django 1.7+:

    1. ./manage.py dumpdata --natural-primary --natural-foreign > dump.json
    2. Change DATABASES in settings.py to point to the new (PostgreSQL) db.
    3. ./manage.py migrate
    4. ./manage.py loaddata dump.json

    One problem I encountered is that SQLite doesn't seem to actually enforce the maximum length for CharFields. In my case, this made the loaddata step fail. I was able to find (and delete) model instances with too long CharField values via:

    MyModel.objects.extra(where=["LENGTH(text) > 20"]).delete()
    

    Once I did this before step 1. above, everything worked.

    0 讨论(0)
提交回复
热议问题