Python 仅将表迁移到Django中2个数据库中的1个
我试图在1个数据库中有选择地创建表,但在另一个数据库中没有 在我的示例中,我只想为应用程序创建表:Python 仅将表迁移到Django中2个数据库中的1个,python,django,Python,Django,我试图在1个数据库中有选择地创建表,但在另一个数据库中没有 在我的示例中,我只想为应用程序创建表:transforms数据库中的tlocation和tcategory。但是,Django正在transforms数据库中创建所有表 以下是DB路由器配置: TRANSFORM_APPS = ('tcategory', 'tlocation') class TransformRouter(object): """ A router to control all database o
transforms
数据库中的tlocation
和tcategory
。但是,Django正在transforms
数据库中创建所有表
以下是DB路由器配置:
TRANSFORM_APPS = ('tcategory', 'tlocation')
class TransformRouter(object):
"""
A router to control all database operations on models in the
"utils_transform" application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read 'transforms' models go to 'transforms' database.
"""
if model._meta.app_label in TRANSFORM_APPS:
return 'transforms'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write 'transforms' models go to 'transforms' database.
"""
if model._meta.app_label in TRANSFORM_APPS:
return 'transforms'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the 'tlocation' app is involved.
"""
if obj1._meta.app_label in TRANSFORM_APPS or \
obj2._meta.app_label in TRANSFORM_APPS:
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
"""
Make sure the 'tlocation' app only appears in the 'transforms'
database.
"""
if app_label in TRANSFORM_APPS:
return db == 'transforms'
return None
class DefaultRouter(object):
"""
Catch-all Router for all other DB transactions that aren't in the
``utils_transform`` app.
"""
def db_for_read(self, model, **hints):
return 'default'
def db_for_write(self, model, **hints):
return 'default'
def allow_relation(self, obj1, obj2, **hints):
if obj1._state.db == obj2._state.db:
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
return None
我用于运行迁移的命令是:
./manage.py migrate --database=transforms
当我一次只迁移一个应用程序时,如下所示,这是可行的。但是如果在migrate
命令中没有应用程序过滤器,我就无法运行。例如:
./manage.py migrate tlocation --database=transforms
./manage.py migrate tcategory --database=transforms
对于不想创建表的模型,您是否尝试过
managed=False
class MyModel(models.Model):
...
class Meta:
managed = False
您确认它实际上正在创建表吗?因为无论哪种情况下,
migrate
的输出看起来都是一样的,所以它“只会在返回False
的模型上默默地不执行任何操作”。@KevinChristopherHenry,yes,它正在创造tables@AaronLelevier数据库路由器设置中路由器的顺序是什么?这也无法区分数据库。它不会在任何数据库中创建表。