Django: Resolving "Table Doesn't Exist" Error
This error occurs when Django attempts to perform database operations on a table that no longer exists, typically due to a manual deletion or a change in the application's models.
Problem Explanation:
After dropping the model-related table, the syncdb command tries to create the table again. However, since the model for the table is still present in models.py, Django expects the table to be there but finds it missing. This results in the "Table doesn't exist" error.
Solution Steps:
Execute migrations (For Django versions >= 1.7):
OR
Execute schema migration (For Django versions < 1.7):
Example for Django versions >= 1.7:
# Comment out the model in models.py # class feed(models.Model): # ... # Execute migrations python manage.py makemigrations python manage.py migrate # Comment in the model in models.py # class feed(models.Model): # ... # Re-execute migrations python manage.py migrate
Example for Django versions < 1.7:
# Comment out the model in models.py # class feed(models.Model): # ... # Execute schema migration python manage.py schemamigration someapp --auto python manage.py migrate someapp --fake # Comment in the model in models.py # class feed(models.Model): # ... # Re-execute schema migration python manage.py migrate someapp
By following these steps, you can recreate the missing tables and resolve the "Table doesn't exist" error.
以上是如何修復 Django 中的「表不存在」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!