首页 > 解决方案 > Django Migrate 表创建不一致 - 多个旧版数据库

问题描述

我是一个新的 Django 用户,我似乎在使用 Django 迁移到我的旧数据库(SQL Server)时创建表时遇到了一些问题。基本上,我有 3 个遗留数据库(comp1_db、comp2_db、comp3_db),我已经按如下方式设置了路由(基本上每个数据库完全相同):

**db_routers.py** 
class comp1Router:
    route_app_labels = {'contenttypes','shared_invoice_detail'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'comp1_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'comp1_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'comp1_db'
        return None

class comp2Router:
    route_app_labels = {'contenttypes','shared_invoice_detail'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'comp2_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth and contenttypes models go to auth_db.
        """
        if model._meta.app_label in self.route_app_labels:
            return 'comp2_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'comp2_db'
        return None

class comp3Router:
    route_app_labels = {'contenttypes','shared_invoice_detail'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'comp3_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth and contenttypes models go to auth_db.
        """
        if model._meta.app_label in self.route_app_labels:
            return 'comp3_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'comp3_db'
        return None

**settings.py** router section
DATABASE_ROUTERS = ['routers.db_routers.comp1Router',
                    'routers.db_routers.comp2Router',
                    'routers.db_routers.comp3Router',
                    ]

**models.py** (part of it)
class DJ_ArInvoiceHandover(models.Model):
    document_no = models.CharField(db_column='Document No', primary_key=True, max_length=20)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    transaction_type = models.CharField(db_column='Transaction Type', max_length=10, default= 'INV' )  # Field name made lowercase. Field renamed to remove unsuitable characters.
    limit = models.Q(app_label = 'shared_invoice_detail', model='ArInvoiceMain')|models.Q(app_label = 'shared_invoice_detail', model='ArInvoiceMainHist')
    content_type = models.ForeignKey(ContentType, limit_choices_to = limit, on_delete=models.CASCADE, default='')
    content_object = GenericForeignKey('content_type', 'document_no')
    handover_by =  models.CharField(db_column='Handover By', max_length=20, blank=True, null=False, default='')
    handover_datetime = models.DateTimeField(db_column='Handover DateTime', blank=True, null=True)
    last_updated = models.DateTimeField(db_column = 'Last Updated', auto_now=True)

    def __str_(self):
        return self.document_no

    class Meta:
        managed = True
        db_table = 'DJ_AR_Invoice_Handover'

我从所有 3 个数据库中读取数据都没有问题,但是当我尝试在所有 3 个数据库中创建一个新模型(因此是一个表)时,一切似乎都运行正常。但是,我只看到在 comp1 数据库中创建的表。我不知道问题是什么.....非常感谢您提供任何帮助,谢谢。

python manage.py makemigrations
python manage.py migrate --database=comp1_db (I repeat for comp2 and comp3)
....
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
  Applying shared_invoice_detail.0001_initial... OK
  Applying shared_invoice_detail.0002_dj_arinvoicehandover... OK

标签: pythonsql-serverdjangodjango-migrations

解决方案


推荐阅读