【问题标题】:django.db.utils.ProgrammingError: Table doesn't existdjango.db.utils.ProgrammingError:表不存在
【发布时间】:2018-08-05 20:50:24
【问题描述】:

我使用了两个数据库,一个是默认的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

当我尝试在管理面板中打开帖子表时会发生这种情况

有什么想法吗?

【问题讨论】:

  • 您在屏幕左侧使用什么软件,其中列出了“版本”、“时间”、“设置”等?

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


【解决方案1】:

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

python manage.py makemigrations <application>

之前

python manage.py migrate <application>

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

【讨论】:

    【解决方案2】:

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

    试试

    python manage.py migrate --database=customer
    

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

    【讨论】:

    • 数据库路由器在您的读写操作期间可以工作,但在迁移期间它不会工作。因此,您需要在所有数据库上运行迁移,这是 --database 选项的帮助
    【解决方案3】:

    就我而言,我有两个数据库托管在两个不同的服务器上。这两个数据库都可以使用 MySQL,但它与使用 SQLLite-MySQL 的情况相同。

    也许这个例子可以解决你的问题,所以我在我的 Django Web 应用程序中显示我所拥有的。

    到我的settings.py 文件中类似这样的内容:

    DATABASES = {
    
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'default',
            'USER': 'osx',
            'PASSWORD': '******',
            'HOST': '172.30.10.12',
            'PORT': '3306',
            'OPTIONS': {
                'init_command': 'SET innodb_strict_mode=1',
                'sql_mode': 'traditional',
            }
        },
    
    
        'DS2': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'DS2',
            'USER': 'osx',
            'PASSWORD': '******',
            'HOST': '172.30.10.13',
            'PORT': '3306',
            'OPTIONS': {
                'init_command': 'SET innodb_strict_mode=1',
                'sql_mode': 'traditional',
            }
        }
    }
    
    
    BDD = ('default', 'DS2')
    DATABASE_ROUTERS = ['MyApp.routersLocal.LocalRouter', 'MyApp.routersGlobal.GlobalRouter']
    

    然后我有两个文件:routersGlobal.pyroutersLocal.py

    #routersGlobal.py
    
    from django.conf import settings
    
    class GlobalRouter(object):
        """
    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.
            """
            app_list = ('Identity',)
    
            if model._meta.app_label in app_list:
                return 'DS2'
            return None
    
        def db_for_write(self, model, **hints):
            """
            Attempts to write auth models go to auth.
            """
            app_list = ('Identity',)
            if model._meta.app_label in app_list:
                return 'DS2'
            return None
    
        def allow_relation(self, obj1, obj2, **hints):
            """
            Allow relations if a model in the auth app is involved.
            """
            app_list = ('Identity',)
            if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
                return 'DS2'
            return None
    
        def allow_migrate(self, db, app_label, model=None, **hints):
            """
            Make sure the auth app only appears in the 'auth'
            database.
            """
            app_list = ('Identity',)
    
            if app_label in app_list:
                return db == 'DS2'
            return None
    

    #routersLocal.py
    from django.conf import settings
    
    class LocalRouter(object):
        """
    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.
            """
            app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'Configurations', 'log', 'Home', 'DSCORE', 'Informations')
    
            if model._meta.app_label in app_list:
                return 'default'
            return None
    
        def db_for_write(self, model, **hints):
            """
            Attempts to write auth models go to auth.
            """
            app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'Configurations', 'log', 'Home', 'DSCORE', 'Informations')
            if model._meta.app_label in app_list:
                return 'default'
            return None
    
        def allow_relation(self, obj1, obj2, **hints):
            """
            Allow relations if a model in the auth app is involved.
            """
            app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'Configurations', 'log', 'Home', 'DSCORE', 'Informations')
            if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
                return True
            return None
    
        def allow_migrate(self, db, app_label, model=None, **hints):
            """
            Make sure the auth app only appears in the 'auth'
            database.
            """
            app_list = ('auth', 'admin', 'contenttypes', 'sessions', 'Configurations', 'log', 'Home', 'DSCORE', 'Informations')
    
            if app_label in app_list:
                return db == 'default'
            return None 
    

    编辑

    makemigrationsmigrate 是你制作的吗?

    【讨论】:

    • 是的,我确实迁移了东西。您选择与 DS2 打交道的应用程序是“身份”吗?谢谢,学习你的代码真是太好了。
    • Identity 是我的 Django 应用程序之一。身份数据存储在DS2。来自sessionsadminauth .. 的其他数据存储在我的默认数据库中。这就是为什么我的默认数据库对应Local 数据和我的DS2 数据库对应Global。一些实例可以连接到DS2,以便添加数据、获取数据......但每个实例都有一个本地数据库(在我的情况下是默认的)。
    • 所以你必须在项目目录下(apps目录外)写routersLocal.py?
    • 我将这两个文件都写在主目录(settings.py 所在的位置)而不是模块目录中。
    • 哦,我没有输入我的编辑 --database=yourbase .. :/ 不,我没有博客或 github。自 2 年以来,我一直在开发我的 Django Web 应用程序。
    猜你喜欢
    • 2019-02-18
    • 2021-10-17
    • 1970-01-01
    • 2018-07-08
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多