Python &引用;没有这样的表格”;在Django 1.11.1上使用多个数据库
我在Django 1.11.1中遇到了多个数据库的问题 我总是收到: 操作错误位于/admin/news/article/Python &引用;没有这样的表格”;在Django 1.11.1上使用多个数据库,python,django,Python,Django,我在Django 1.11.1中遇到了多个数据库的问题 我总是收到: 操作错误位于/admin/news/article/ 没有这样的表格:新闻文章 当我点击管理员页面上的“Artikel”时 我的项目包含两个数据库,一个用于Recipes,一个用于RSS新闻提要。 我在设置.py中这样定义了它们: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.p
没有这样的表格:新闻文章 当我点击管理员页面上的“Artikel”时 我的项目包含两个数据库,一个用于Recipes,一个用于RSS新闻提要。 我在
设置.py中这样定义了它们:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', 'cookbook.db'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'news_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', 'news.db'),
},
}
class CookbookRouter(object):
"""
A router to control all database operations on models in the
cookbook site.
"""
def db_for_read(self, model, **hints):
if model._meta.app_label == 'news':
return 'news_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'news':
return 'news_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'news' or \
obj2._meta.app_label == 'news':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'news':
return db == 'news_db'
return None
DATABASE_ROUTERS = ['cookbook.routers.CookbookRouter']
我还在routers.py
中添加了一个路由器:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', 'cookbook.db'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'news_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', 'news.db'),
},
}
class CookbookRouter(object):
"""
A router to control all database operations on models in the
cookbook site.
"""
def db_for_read(self, model, **hints):
if model._meta.app_label == 'news':
return 'news_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'news':
return 'news_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'news' or \
obj2._meta.app_label == 'news':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'news':
return db == 'news_db'
return None
DATABASE_ROUTERS = ['cookbook.routers.CookbookRouter']
我在settings.py中这样设置它:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', 'cookbook.db'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'news_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, '..', 'news.db'),
},
}
class CookbookRouter(object):
"""
A router to control all database operations on models in the
cookbook site.
"""
def db_for_read(self, model, **hints):
if model._meta.app_label == 'news':
return 'news_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'news':
return 'news_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'news' or \
obj2._meta.app_label == 'news':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'news':
return db == 'news_db'
return None
DATABASE_ROUTERS = ['cookbook.routers.CookbookRouter']
我的news.model.py如下所示:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from basemodels import DateTimeInfo
# Create your models here.
class Article(DateTimeInfo):
headline = models.CharField(u'Überschrift', max_length=100)
body = models.TextField(u'Inhalt')
class Meta:
verbose_name = u'Artikel'
verbose_name_plural = u'Artikel'
ordering = ['-date_updated']
def __unicode__(self):
return self.headline
跑步后
$python manage.py makemigration
$python manage.py migrate
几次让舒尔知道,如果我跑步,桌上的文章就会出现
$python manage.py检查数据库
输出:
我做错了什么 您是否手动创建了“新闻文章”?它是旧表吗?如何检查它是否是旧表?(什么是遗留表)我认为运行“makemigrations”和“migrate”将创建models.py中定义的所有表,同时定义数据库路由器。现在我通过调用:$python manage.py migrate--database=news\u articleBy legacy table解决了这个问题,我的意思是“一个已经存在并且需要在系统中使用的表”makemigrations
不会创建表或删除非托管表,因此请删除managed=false
或手动创建。您正在使用sqlite。这意味着这个项目肯定不足以保证两个不同的数据库。只要用一个,你的问题就会消失