【问题标题】:No such table Django Database没有这样的表 Django 数据库
【发布时间】:2018-10-24 09:53:30
【问题描述】:

所以我创建了一个模型来存储来自 Gmail 用户的凭据。 我想进行迁移,但它说没有这样的表:

django.db.utils.OperationalError: no such table: mainApp_credentialsmodel

我的模型:

from django.db import models

# Create your models here.

from django.contrib.auth.models import User
from django.db import models
import json


class CredentialsModel(models.Model):
  id = models.ForeignKey(User, primary_key=True,on_delete=models.CASCADE)
  credential = models.CharField(max_length=1000)

调用该模型来检查授权:

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

store = CredentialsModel.objects.all()
creds = store.get()


if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('mainApp/client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))

【问题讨论】:

  • 你能发布你在这个应用程序中的最后一个迁移文件mainApp_credentialsmodel吗?

标签: sql django database sqlite django-models


【解决方案1】:

您的授权代码(包括对 CredentialsModel 的查询)似乎处于模块级别。这意味着它在导入模块时运行,这发生在迁移有机会运行之前。

您必须确保任何数据库访问代码都在函数或方法内,并且不会被全局调用。

【讨论】:

  • 我删除了迁移目录中的所有内容并删除了我的 .slite3 数据库文件,还从“quickstart.py”中删除了所有导入的模型并运行了“makemigrations”、“migrate”。一切顺利。在此之后,我将 CredentialsModel 导入“quickstart.py”并尝试再次迁移,但它显示:mainApp.models.DoesNotExist: CredentialsModel 匹配查询不存在。
【解决方案2】:

python manage.py makemigrations 如果该错误继续发生,请检查您的迁移文件夹并检查其中的文件。还要检查如果您的数据库在线,以防您有在线数据库,我上周遇到了这个问题,但这是 azure 的问题。 在最后一种情况下,我会再次创建表(模型),将名称更改为类似的名称,但如果该表中有大量数据,那么我认为你不能这样做。

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 2014-01-06
    • 1970-01-01
    • 2012-09-28
    • 2021-12-10
    • 1970-01-01
    • 2019-08-13
    • 2021-08-18
    • 2019-05-07
    相关资源
    最近更新 更多