django.db.utils.ProgrammingError : Table doesn't exist

标签 django django-models mysql-python django-database

我使用了两个数据库,一个是默认的sqlite3,另一个是mysql。我刚刚在名为 theapp 的应用程序下创建了一个模型帖子。我还创建了 routers.py。

这是settings.py

DATABASES = {
    'default': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },

    'customer': {
       'NAME': 'customer',
       'ENGINE': 'django.db.backends.mysql',
       'USER': 'root',
       'PASSWORD': 'haha',
    }
 }

这是routers.py

class CustomerRouter:
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
    """
    Attempts to read auth models go to auth_db.
    """
    if model._meta.app_label == 'theapp':
        return 'customer'
    return None

def db_for_write(self, model, **hints):
    """
    Attempts to write auth models go to auth_db.
    """
    if model._meta.app_label == 'theapp':
        return 'customer'
    return None

def allow_relation(self, obj1, obj2, **hints):
    """
    Allow relations if a model in the auth app is involved.
    """        
    if obj1._meta.app_label == 'theapp' or \
       obj2._meta.app_label == 'theapp':
       return True
    return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
    """
    Make sure the auth app only appears in the 'auth_db'
    database.
    """
    if app_label == 'theapp':
        return db == 'customer'
    return None

这是我的models.py

from django.db import models
from django.utils import timezone


class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

这就是我尝试在管理面板中打开帖子表时发生的情况

enter image description here

有什么想法吗?

最佳答案

我认为您尚未在 MySQL 数据库上运行迁移,

尝试

python manage.py migrate --database=customer

阅读https://docs.djangoproject.com/en/1.11/ref/django-admin/#migrate

关于django.db.utils.ProgrammingError : Table doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48987807/

相关文章:

python - AUTH_USER_MODEL 不接受子申请

python - Django - 从一个用户创建的模型对象对所有其他用户可见

python - 在 django ORM 中查询时如何将 char 转换为整数?

Django认证: Creation of objects by users before registration

python mysqldb导入错误

Python MySQLdb : How to get the result of a sql select having a group by into a dict?

django - 当我不知道我将收到的结构时,如何循环遍历 Django 中的嵌套上下文字典

python - 如何追踪这个?属性错误 : 'NoneType' object has no attribute 'is_relation' during makemigrations

python - Django ORM 与 Postgres : rows unexpectedly deleted - Bug?

python - Django 持久数据库连接 - 一种不同的方法