Django-为什么在使用外壳程序时为什么会出现OperationalError(仅)?

时间:2018-07-17 18:27:02

标签: python django python-3.x

我遇到了一个奇怪的情况。怎么了?

当我从项目目录运行python manage.py shell时,导入一个模型(任何模型),并尝试访问其Model.objectsobjects.all()objects.create(),... ),则抛出一个OperationalError,如下所述。

>>> from Interface.models import ClientUser
>>> ClientUser.objects.all()
  

django.db.utils.OperationalError:没有这样的表:Interface_clientuser

>>> ClientUser.objects.create()
  

django.db.utils.OperationalError:没有这样的表:Interface_clientuser

我尝试删除数据库,然后运行python manage.py migrate,关闭所有内容并重新启动计算机,并非常努力地盯着代码(lol),但无济于事。

有趣的是,我的单元测试通过了;包括例如调用ClientUser.objects.create()

这是ClientUser模型,尽管我认为该模型不是问题所在(所有模型都发生错误,并且单元测试通过。)

class ClientUser(models.Model):
    guid = models.UUIDField(null=True, unique=True)
    username = models.CharField(max_length=256, null=True, unique=True)
    first_name = models.CharField(max_length=256, null=True)
    last_name = models.CharField(max_length=256, null=True)
    email = models.CharField(max_length=256, null=True)

    def most_recent_device(self):
        return self.devices.order_by('-pk').first()

    # I just put this classmethod in. I'm not sure if this will work, and went to the shell to try it, and that's when I noticed this issue. I've tried commenting it out and it makes no difference.    
    @classmethod
    def get_by_guid(cls, guid):
        return cls.objects.get(guid=guid)

这是来自外壳的完整堆栈跟踪(上图):

>>> from Interface.models import ClientUser
>>> ClientUser.objects.all()
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: Interface_clientuser

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

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\query.py", line 248, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\query.py", line 272, in __iter__
    self._fetch_all()
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1068, in execute_sql
    cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Program Files\Python37\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:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: Interface_clientuser

2 个答案:

答案 0 :(得分:1)

您的表格未创建...

运行makemigrations来检查是否存在要应用的任何迁移

python manage.py makemigrations

然后运行migrate将所有迁移应用到数据库中

python manage.py migrate

编辑:您使用了多少个数据库? sqlite也会引发此错误,是否在设置和测试中使用sqlite?

答案 1 :(得分:0)

除了以下以外,还需要其他魔术:

python manage.py makemigrations
python manage.py migrate

运行这个奥秘的野兽:

python manage.py migrate --run-syncdb

高兴!:

C:\Code\Py\django\HealthCheck>python manage.py migrate --run-syncdb
Operations to perform:
  Synchronize unmigrated apps: Interface, messages, rest_framework, staticfiles
  Apply all migrations: admin, auth, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table Interface_clientuser
    Creating table Interface_clientgroup
    Creating table Interface_device
    Creating table Interface_supportrequest
    Creating table Interface_fcmmessage
    Creating table Interface_healthcheckalert
    Running deferred SQL...
Running migrations:
  No migrations to apply.

C:\Code\Py\django\HealthCheck>python manage.py shell
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from Interface.models import ClientUser
>>> ClientUser.objects.all()
<QuerySet []>