我们有一个使用数据库路由器的多数据库Django项目。
出于某种原因,当我们运行迁移时,对迁移的引用最终出现在django_migrations表中,但实际上没有运行任何实际的迁移—也就是说,目标数据库中没有任何更改。
以下是数据库路由器
elegant
数据库
class ElegantRouter:
"""
A router to control all database operations on models in the
elegant application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read elegant models go to elegant.
"""
if model._meta.app_label == 'elegant':
return 'elegant'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write elegant models go to elegant.
"""
if model._meta.app_label == 'elegant':
return 'elegant'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the elegant app is involved.
"""
if obj1._meta.app_label == 'elegant' or \
obj2._meta.app_label == 'elegant':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the elegant app only appears in the 'elegant'
database.
"""
print('allow_migrate',app_label,db)
if app_label == 'elegant':
return db == 'elegant'
return None
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'depot_maestro': {
'ENGINE': 'django_filemaker_pyodbc',
'HOST': os.getenv('FILEMAKER_HOST'),
'PORT': os.getenv('FILEMAKER_PORT'),
'USER': os.getenv('FILEMAKER_USER'),
'PASSWORD': os.getenv('FILEMAKER_PASSWORD'),
'NAME': os.getenv('FILEMAKER_FILENAME'),
'OPTIONS' : {
'driver' : os.getenv('FILEMAKER_DRIVER'),
'driver_supports_utf8' : True,
'autocommit' : True ,
},
'TEST': {
'NAME': os.getenv('FILEMAKER_FILENAME'),
'SERIALIZE': False,
}
},
'postgres': {
'NAME': os.getenv('POSTGRES_DATABASE'),
'ENGINE': 'django.db.backends.postgresql',
'USER': os.getenv('POSTGRES_USER'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD')
},
'elegant': {
'NAME': os.getenv('ELEGANT_DATABASE'),
'ENGINE': 'django.db.backends.postgresql',
'USER': os.getenv('ELEGANT_USER'),
'PASSWORD': os.getenv('ELEGANT_PASSWORD')
},
}
使现代化
使用
sqlmigrate
./manage.py sqlmigrate elegant 0057 --database elegant
BEGIN;
--
-- Remove field internal_id from organisationelegant
--
--
-- Add field uuid to organisationelegant
--
--
-- Alter field id on organisationelegant
--
COMMIT;
其中,没有数据库路由器的项目返回SQL
./manage.py sqlmigrate elegant 0057
BEGIN;
--
-- Remove field internal_id from organisationelegant
--
ALTER TABLE "organisation" DROP COLUMN "id" CASCADE;
...
--
-- Alter field id on organisationelegant
--
DROP INDEX IF EXISTS "organisation_uuid_19796862_like";
COMMIT;
当allow migrate已返回true且正在生成迁移文件时,如何使使用数据库路由器的迁移生效?