Django 应用分库,数据迁移成功,数据库没有生成表
生活随笔
收集整理的這篇文章主要介紹了
Django 应用分库,数据迁移成功,数据库没有生成表
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Django 應(yīng)用分庫(kù),數(shù)據(jù)遷移成功,數(shù)據(jù)庫(kù)沒(méi)有生成表
背景:不同應(yīng)用對(duì)應(yīng)不同數(shù)據(jù)庫(kù),在遷移數(shù)據(jù)成功后,數(shù)據(jù)庫(kù)沒(méi)有生成表
Django 官網(wǎng):https://docs.djangoproject.com/ko/1.11/topics/db/multi-db/#allow_migrate
多個(gè)數(shù)據(jù)庫(kù)有介紹
settings.py 設(shè)置
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,}, } #數(shù)據(jù)庫(kù)設(shè)置 DATABASE_ROUTERS = ['employee_service0001.db_router.database_router'] DATABASE_APPS_MAPPING = {'platform_***':'default','data_***':'data_***', }settings.py 同級(jí)文件創(chuàng)建 數(shù)據(jù)庫(kù)路由分發(fā) db_router:
from .settings import DATABASE_APPS_MAPPINGDATABASE_MAPPING = DATABASE_APPS_MAPPING# 數(shù)據(jù)庫(kù)路由分發(fā) class database_router(object):def db_for_read(self, model, **hints):""""Point all read operations to the specific database.""""""將所有讀操作指向特定的數(shù)據(jù)庫(kù)。"""if model._meta.app_label in DATABASE_MAPPING:return DATABASE_MAPPING[model._meta.app_label]return Nonedef db_for_write(self, model, **hints):"""Point all write operations to the specific database.""""""將所有寫(xiě)操作指向特定的數(shù)據(jù)庫(kù)。"""if model._meta.app_label in DATABASE_MAPPING:return DATABASE_MAPPING[model._meta.app_label]return Nonedef allow_relation(self, obj1, obj2, **hints):"""Allow any relation between apps that use the same database.""""""允許使用相同數(shù)據(jù)庫(kù)的應(yīng)用程序之間的任何關(guān)系"""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 Trueelse:return Falseelse:return Nonedef allow_syncdb(self, db, model):"""Make sure that apps only appear in the related database.""""""確保這些應(yīng)用程序只出現(xiàn)在相關(guān)的數(shù)據(jù)庫(kù)中。"""if db in DATABASE_MAPPING.values():return DATABASE_MAPPING.get(model._meta.app_label) == dbelif model._meta.app_label in DATABASE_MAPPING:return Falsereturn Nonedef allow_migrate(self, db, app_label, model=None, **hints):"""Make sure the auth app only appears in the 'auth_db' database.""""""確保身份驗(yàn)證應(yīng)用程序只出現(xiàn)在“authdb”數(shù)據(jù)庫(kù)中。"""if db in DATABASE_MAPPING.values():return DATABASE_MAPPING.get(app_label) == dbelif app_label in DATABASE_MAPPING:return Falsereturn None模型文件
data_***/models.py:
from django.db import modelsclass IntelligentTableView(models.Model):table_name = models.CharField(max_length=255, verbose_name='報(bào)表名稱')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='創(chuàng)建人')create_date = models.DateTimeField(auto_now=True, verbose_name='創(chuàng)建時(shí)間')is_del = models.BooleanField(default=False, verbose_name='是否刪除')class Meta:db_table = 'intelligent_table' # 表名verbose_name = '智能報(bào)表'app_label = 'data_center' # 指明app名稱,用來(lái)對(duì)應(yīng)app 和 數(shù)據(jù)庫(kù)的map表遷移文件
python manage.py makemigrations python manage.py migrate遷移成功,但是數(shù)據(jù)庫(kù)沒(méi)有生成對(duì)應(yīng)的表,因?yàn)槎紩?huì)跑到默認(rèn)數(shù)據(jù)庫(kù)中
在遷移數(shù)據(jù)時(shí) 添加應(yīng)用名稱:
python manage.py migrate --database=data_center總結(jié)
以上是生活随笔為你收集整理的Django 应用分库,数据迁移成功,数据库没有生成表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: duilib消息事件产生和分发解释
- 下一篇: python多进程并发与pool多线程