mysql 为什么当我尝试在Django 3.2.6上进行迁移时会出现这个错误?

1wnzp6jl  于 2023-02-11  发布在  Mysql
关注(0)|答案(1)|浏览(169)

我想用命令更新我的数据库

python manage.py makemigrations

之后我删除了Django项目中所有的迁移文件,因为当我尝试在我的数据库上做修改时,这些修改并不适用
这是我的模特

class ciudades(models.Model):
    ciudad_nombre = models.CharField(max_length=50)
    estado = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'ciudades'
        verbose_name ='Ciudad'
        verbose_name_plural = 'Ciudades'

这是我得到的错误

Traceback (most recent call last):
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 22, in <module>
    main()
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/config.py", line 124, in create
    mod = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/apps.py", line 8, in <module>
    from .checks import check_models_permissions, check_user_model
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/checks.py", line 8, in <module>
    from .management import _get_builtin_permissions
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/management/__init__.py", line 9, in <module>
    from django.contrib.contenttypes.management import create_contenttypes
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/contenttypes/management/__init__.py", line 2, in <module>
    from django.db import (
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/migrations/__init__.py", line 1, in <module>
    from .migration import Migration, swappable_dependency  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'django.db.migrations.migration'
❯ python manage.py makemigrations
Traceback (most recent call last):
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 22, in <module>
    main()
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/config.py", line 124, in create
    mod = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/apps.py", line 8, in <module>
    from .checks import check_models_permissions, check_user_model
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/checks.py", line 8, in <module>
    from .management import _get_builtin_permissions
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/management/__init__.py", line 9, in <module>
    from django.contrib.contenttypes.management import create_contenttypes
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/contenttypes/management/__init__.py", line 2, in <module>
    from django.db import (
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/migrations/__init__.py", line 1, in <module>
    from .migration import Migration, swappable_dependency  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'django.db.migrations.migration'
❯ python3 manage.py makemigrations
Traceback (most recent call last):
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 22, in <module>
    main()
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/config.py", line 124, in create
    mod = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/apps.py", line 8, in <module>
    from .checks import check_models_permissions, check_user_model
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/checks.py", line 8, in <module>
    from .management import _get_builtin_permissions
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/auth/management/__init__.py", line 9, in <module>
    from django.contrib.contenttypes.management import create_contenttypes
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/contrib/contenttypes/management/__init__.py", line 2, in <module>
    from django.db import (
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/migrations/__init__.py", line 1, in <module>
    from .migration import Migration, swappable_dependency  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'django.db.migrations.migration'
❯ python -m django --version
3.2.6
❯ pip install --upgrade --force-reinstall  Django==3.2.6
Collecting Django==3.2.6
  Using cached Django-3.2.6-py3-none-any.whl (7.9 MB)
Collecting asgiref<4,>=3.3.2
  Downloading asgiref-3.6.0-py3-none-any.whl (23 kB)
Collecting pytz
  Downloading pytz-2022.7.1-py2.py3-none-any.whl (499 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 499.4/499.4 kB 3.0 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.3-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.8/42.8 kB 3.6 MB/s eta 0:00:00
Installing collected packages: pytz, sqlparse, asgiref, Django
  Attempting uninstall: pytz
    Found existing installation: pytz 2021.1
    Uninstalling pytz-2021.1:
      Successfully uninstalled pytz-2021.1
  Attempting uninstall: sqlparse
    Found existing installation: sqlparse 0.4.2
    Uninstalling sqlparse-0.4.2:
      Successfully uninstalled sqlparse-0.4.2
  Attempting uninstall: asgiref
    Found existing installation: asgiref 3.4.1
    Uninstalling asgiref-3.4.1:
      Successfully uninstalled asgiref-3.4.1
  Attempting uninstall: Django
    Found existing installation: Django 3.2.6
    Uninstalling Django-3.2.6:
      Successfully uninstalled Django-3.2.6
Successfully installed Django-3.2.6 asgiref-3.6.0 pytz-2022.7.1 sqlparse-0.4.3

[notice] A new release of pip available: 22.3.1 -> 23.0
❯ python manage.py makemigrations
Traceback (most recent call last):
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
          ^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.ProgrammingError: (1146, "Table 'dohaerp_db.ciudades' doesn't exist")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 22, in <module>
    main()
  File "/Users/dorianraygoza/Downloads/doha-erp/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/dorianraygoza/Downloads/doha-erp/apps/home/models.py", line 197, in <module>
    for i in ciudades.objects.all():
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/models/query.py", line 280, in __iter__
    self._fetch_all()
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
          ^^^^^^^^^^^^^^^^^^
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/Users/dorianraygoza/Downloads/doha-erp/venv/lib/python3.11/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'dohaerp_db.ciudades' doesn't exist")

我删除所有迁移文件以更新数据库,但仍然无法工作
我尝试创建新数据库以运行新迁移,但仍然出现相同的错误

eagi6jfj

eagi6jfj1#

我已经解决了
这是对模型文件的查询,lain Shelvington注解帮助我找到此错误

for i in ciudades.objects.all():
origen_quotes.append((i.ciudad_nombre, i.ciudad_nombre))    
destino_quotes.append((i.ciudad_nombre, i.ciudad_nombre))

因此,我只对代码的这一部分进行注解,它工作正常,这是因为我使用的这个表是在makemigrations命令之后创建的,因此CRM找不到该表,但当我注解并运行该命令时

python manage.py makemigrations

以及

python manage.py migrate

很管用

相关问题