Django 应用分库,数据迁移成功,数据库没有生成表
背景:不同应用对应不同数据库,在迁移数据成功后,数据库没有生成表
Django 官网:https://docs.djangoproject.com/ko/1.11/topics/db/multi-db/#allow_migrate
多个数据库有介绍
settings.py
设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'employee_01',
'USER':'root',
'PASSWORD':'Zhb123321@',
'HOST':'localhost',
'PORT':3306,
},
'data_***': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'data_***_01',
'USER': 'root',
'PASSWORD': 'Zhb123321@',
'HOST': 'localhost',
'PORT': 3306,
},
}
#数据库设置
DATABASE_ROUTERS = ['employee_service0001.db_router.database_router']
DATABASE_APPS_MAPPING = {
'platform_***':'default',
'data_***':'data_***',
}
settings.py
同级文件创建 数据库路由分发 db_router
:
from .settings import DATABASE_APPS_MAPPING
DATABASE_MAPPING = DATABASE_APPS_MAPPING
# 数据库路由分发
class database_router(object):
def db_for_read(self, model, **hints):
""""Point all read operations to the specific database."""
"""将所有读操作指向特定的数据库。"""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None
def db_for_write(self, model, **hints):
"""Point all write operations to the specific database."""
"""将所有写操作指向特定的数据库。"""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None
def allow_relation(self, obj1, obj2, **hints):
"""Allow any relation between apps that use the same database."""
"""允许使用相同数据库的应用程序之间的任何关系"""
db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)
db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)
if db_obj1 and db_obj2:
if db_obj1 == db_obj2:
return True
else:
return False
else:
return None
def allow_syncdb(self, db, model):
"""Make sure that apps only appear in the related database."""
"""确保这些应用程序只出现在相关的数据库中。"""
if db in DATABASE_MAPPING.values():
return DATABASE_MAPPING.get(model._meta.app_label) == db
elif model._meta.app_label in DATABASE_MAPPING:
return False
return None
def allow_migrate(self, db, app_label, model=None, **hints):
"""Make sure the auth app only appears in the 'auth_db' database."""
"""确保身份验证应用程序只出现在“authdb”数据库中。"""
if db in DATABASE_MAPPING.values():
return DATABASE_MAPPING.get(app_label) == db
elif app_label in DATABASE_MAPPING:
return False
return None
模型文件
data_***/models.py
:
from django.db import models
class IntelligentTableView(models.Model):
table_name = models.CharField(max_length=255, verbose_name='报表名称')
center = models.CharField(max_length=50, verbose_name='所属中心')
on_line = models.CharField(max_length=50, verbose_name='所属线条')
remake = models.CharField(max_length=255, verbose_name='备注')
create_name = models.CharField(max_length=50, verbose_name='创建人')
create_date = models.DateTimeField(auto_now=True, verbose_name='创建时间')
is_del = models.BooleanField(default=False, verbose_name='是否删除')
class Meta:
db_table = 'intelligent_table' # 表名
verbose_name = '智能报表'
app_label = 'data_center' # 指明app名称,用来对应app 和 数据库的map表
迁移文件
python manage.py makemigrations
python manage.py migrate
迁移成功,但是数据库没有生成对应的表,因为都会跑到默认数据库中
在迁移数据时 添加应用名称:
python manage.py migrate --database=data_center