如何修復 Django 中的「表不存在」錯誤?

Barbara Streisand
發布: 2024-11-14 22:38:02
原創
866 人瀏覽過

How to Fix the

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:

  1. Drop tables (You've already done this).
  2. Comment out the model in models.py.
  3. Execute migrations (For Django versions >= 1.7):

    • python manage.py makemigrations
    • python manage.py migrate --fake

    OR

    Execute schema migration (For Django versions < 1.7):

    • python manage.py schemamigration someapp --auto
    • python manage.py migrate someapp --fake
  4. Comment in the model in models.py.
  5. Re-execute migrations or schema migration (this time without --fake).

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中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板