Django在两个数据库中创建所有模型表 [英] Django creates all model tables in both databases

查看:56
本文介绍了Django在两个数据库中创建所有模型表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django项目中建立了两个数据库,一个应用程序将其数据写入一个数据库,第二个数据库-写入另一个数据库.但是,当我进行迁移时,两个应用程序中的所有模型都在两个数据库中创建了它们的表,尽管它们仍然为空(每个表中只有一个空行)

I made two databases in my django project, one app writes it's data to one database, second - to other. But when I make migration, all models from both apps create their tables in both databases, though they still remain empty (just one null row in every table)

是否可以告诉Django(Django,不要这样做=)不要创建我在两个数据库中都不需要的额外表?

Is it possible to tell Django (Django, don't do it=) not to create extra tables which I don't need in both databases?

这是我的代码

routers.py

    from django.conf import settings

class DatabaseAppsRouter(object):

def db_for_read(self, model, **hints):
    """"Point all read operations to the specific database."""
    if model._meta.app_label in settings.DATABASE_APPS_MAPPING:
        return settings.DATABASE_APPS_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 settings.DATABASE_APPS_MAPPING:
        return settings.DATABASE_APPS_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 = settings.DATABASE_APPS_MAPPING.get(obj1._meta.app_label)
    db_obj2 = settings.DATABASE_APPS_MAPPING.get(obj2._meta.app_label)
    if db_obj1 and db_obj2:
        if db_obj1 == db_obj2:
            return True
        else:
            return False
    return None

def allow_syncdb(self, db, model):
    """Make sure that apps only appear in the related database."""
    if db in settings.DATABASE_APPS_MAPPING.values():
        return settings.DATABASE_APPS_MAPPING.get(model._meta.app_label) == db
    elif model._meta.app_label in settings.DATABASE_APPS_MAPPING:
        return False
    return None

settings.py

    DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

},
'tracking_db': {
     'ENGINE': 'django.db.backends.mysql',
     'NAME': 'tracking',
     'USER': 'root',
     'PASSWORD': '12345678',
}
}

 DATABASE_APPS_MAPPING = {
'contenttypes': 'default',
'auth': 'default',
'admin': 'default',
'sessions': 'default',
'messages': 'default',
'staticfiles': 'default',
'news': 'default',

'tracking': 'tracking_db',
 }
DATABASE_ROUTERS = ['newswallproj.routers.DatabaseAppsRouter']

models.py

Model here
    class Meta:
        app_label = 'tracking'

推荐答案

不幸的是,这只是迁移系统当前的工作方式.对此有一个未解决问题.

Unfortunately no, this is just how the migrations system currently works. There's an open issue about this that you might find helpful.

如评论6所述,可能的部分解决方法是在进行迁移时指定应用程序:

As noted in comment 6, a possible partial workaround is to specify the app when you do the migrations:

./manage.py migrate inapppurchase --database=iap
./manage.py migrate myapp --database=default

但这绝对不是一个可靠的解决方案.

That's definitely not a robust solution, though.

这篇关于Django在两个数据库中创建所有模型表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆