django.db.utils.ProgrammingError: Table doesn't exist
django.db.utils.ProgrammingError: Table doesn't exist
我使用了两个数据库,一个是默认的sqlite3
,另一个是mysql。我刚刚在名为 theapp
的应用程序下创建了一个模型 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
这就是我尝试在管理面板table中打开帖子时发生的情况
有什么想法吗?
就我而言,我有两个数据库托管在两个不同的服务器上。两个数据库都与 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.py
和 routersLocal.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
编辑 :
你做了 makemigrations
和 migrate
了吗?
我认为您的 MySQL 数据库没有 运行 迁移,
尝试
python manage.py migrate --database=customer
阅读https://docs.djangoproject.com/en/1.11/ref/django-admin/#migrate
出于某种原因,Django 有时会要求您这样做
python manage.py makemigrations <application>
之前
python manage.py migrate <application>
确保在 运行 命令之前删除 migrations
和 __pychache__
文件夹。
我使用了两个数据库,一个是默认的sqlite3
,另一个是mysql。我刚刚在名为 theapp
的应用程序下创建了一个模型 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
这就是我尝试在管理面板table中打开帖子时发生的情况
有什么想法吗?
就我而言,我有两个数据库托管在两个不同的服务器上。两个数据库都与 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.py
和 routersLocal.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
编辑 :
你做了 makemigrations
和 migrate
了吗?
我认为您的 MySQL 数据库没有 运行 迁移,
尝试
python manage.py migrate --database=customer
阅读https://docs.djangoproject.com/en/1.11/ref/django-admin/#migrate
出于某种原因,Django 有时会要求您这样做
python manage.py makemigrations <application>
之前
python manage.py migrate <application>
确保在 运行 命令之前删除 migrations
和 __pychache__
文件夹。