django.db.utils.ProgrammingError:表格不存在

时间:2018-02-26 11:46:24

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

我使用了两个数据库,一个是默认的sqlite3,另一个是mysql。我刚刚在名为theapp的app下创建了一个模型Post。我也创建了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

任何想法?

2 个答案:

答案 0 :(得分:2)

我认为你没有在MySQL数据库上运行迁移,

尝试

ng-model

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

答案 1 :(得分:0)

出于某种原因,Django有时会要求您这样做

python manage.py makemigrations <application>

之前

python manage.py migrate <application>

在运行命令之前,请确保删除migrations__pychache__文件夹。