【问题标题】:django return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table:django return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table:
【发布时间】:2021-01-16 07:47:04
【问题描述】:

我有一个简单的模型类如下

class Language(models.Model):
    name= models.CharField(max_length=20)
    code= models.CharField(max_length=5)
    status=models.BooleanField()
    create_at=models.DateTimeField(auto_now_add=True)
    update_at=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

我想这样使用它

llist = Language.objects.filter()
list1 = []
for rs in llist:
    list1.append((rs.code,rs.name))
langlist = (list1)

但是当我尝试使用 llist 时它一直抛出这个错误:

return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: home_language

我尝试使用python manage.py shell 进行查询,它显示与上述相同的错误。

这是完整的错误:

Traceback (most recent call last):
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: home_language

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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\users\daisy\appdata\local\programs\python\python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Daisy\OneDrive\Documents\Work\django\prjs\funnystore\mysite\home\models.py", line 29, in <module>
    for rs in llist:
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\query.py", line 276, in __iter__
    self._fetch_all()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\sql\compiler.py", line 1152, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: home_language

【问题讨论】:

  • 对于尚未进行任何迁移的新应用,您需要将应用名称传递给 makemigrations 以创建初始迁移文件:python manage.py makemigrations home。然后运行python manage.py migrate
  • 尝试将类名更改为language,然后使用llist = language.objects.filter(),也不要忘记再次makemigrationsmigrate
  • 我尝试了上面的建议,但是这个错误发生在迁移之前,所以它不起作用
  • @Knight“这个错误发生在迁移之前”:你的意思是你在尝试创建迁移时遇到错误吗?如果是这样,您需要移动此代码,使其不会在您的应用启动时执行
  • @Knight 此代码在您的 models.py 文件中,在加载文件时执行查询,迁移将加载导致错误的文件。从您的 models.py 中删除代码,您应该能够迁移。为什么它首先位于该文件的根目录中?

标签: django database


【解决方案1】:

在命令提示符下使用此代码:

python manage.py migrate --run-syncdb

这对我有用……..

【讨论】:

  • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
【解决方案2】:

我将迁移用于特定应用,在您的情况下是:

python3 manage.py makemigrations home
python3 manage.py migrate

【讨论】:

    【解决方案3】:

    您应该使用以下方法进行迁移并迁移数据库:

    python manage.py makemigrations
    python manage.py migrate
    

    【讨论】:

      【解决方案4】:

      在我的情况下,通过提前通过管理面板删除数据解决了错误。 那么:

      python manage.py makemigrations
      python manage.py migrate
      

      【讨论】:

        【解决方案5】:

        如果您在任何情况下使用此模型(home_language),请从您的所有代码中删除它,然后迁移它:

        python3 manage.py makemigrations
        python3 manage.py migrate
        

        我希望它应该工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-26
          • 2019-07-04
          • 1970-01-01
          • 2020-11-07
          • 2021-08-03
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多