如何在Django 2.2 ORM中的2个数据库之间创建关系(1个非托管和1个托管)

时间:2019-05-08 13:52:56

标签: django django-models django-2.2

我有两个模型:

  1. ciad_db-MySQL,不受管理
class NormalizedData(models.Model):
    ticket_number = models.CharField(max_length=100, blank=True, primary_key=True, default='noticketid')
    status = models.CharField(max_length=45, blank=True, null=True)
    ticket_source = models.CharField(max_length=45, blank=True, null=True)
    summary = models.TextField(blank=True, null=True)
    conclusion = models.ForeignKey(DsrConclusions, on_delete=models.CASCADE, default=None)

    class Meta:
        managed = False
        db_table = 'normalized_data'
        indexes = [
            models.Index(
                fields=[
                    'status',
                    'ticket_source',
                    'summary',
                ]
            )
        ]
  1. default-sqlite3,托管的
class DsrConclusions(models.Model):
    id = models.IntegerField(primary_key=True)
    ticket_number = models.CharField(max_length=100, blank=False, null=False, unique=True)
    explanation = models.TextField(blank=False, null=False)
    final_breach_conclusion = models.BooleanField(blank=False,null=False)
    last_owner = models.CharField(blank=False,null=False, max_length=50)

    class Meta:
        managed = True
        indexes = [
            models.Index(
                fields=[
                    'ticket_number',
                    'explanation',
                    'final_breach_conclusion',
                    'last_owner',
                ]
            )
        ]

models.py中,DsrConclusionsNormalizedData之前出现

我使用一个数据库路由器是因为有两个数据库,如下所示:

class CiadRouter(object):
    """
    A router to control all database operations on models in
    the ciadseeweb application
    """

    def db_for_read(self, model, **hints):
        """
        Point all operations on ciadseeweb models to 'ciad_db'
        """
        if model._meta.app_label == 'ciadseeweb':
            return 'ciad_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Point all operations on myapp models to 'other'
        """
        if model._meta.app_label == 'ciadseeweb':
            return 'default'
        return None

    def allow_syncdb(self, db, model):
        """
        Make sure the 'ciadseeweb' app only appears on the 'other' db
        """
        if db == 'ciad_db':
            return model._meta.app_label == 'ciadseeweb'
        elif model._meta.app_label == 'ciadseeweb':
            return False
        return None
    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        db_list = ('default', 'ciad_db')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

由于我在迁移完成后添加了该列(并且一切正常),所以一旦添加该列并运行makemigrations,我就会得到No changes detected,这是因为{{1} }不受管理。然后,将其更改为托管,再次运行NormalizedData,让他们检测到更改。一旦我运行makemigrations,它们就可以正常使用了。

当我运行服务器(启动时没有错误)并刷新页面时,我正在使用migrate表将NormalizedData数据渲染到django-tables2中。

为了从django_tables2_column_shifter模型中呈现的DsrConclusions模型中的tables.py模型中获取数据。我正在使用NormalizedData文档中指定的dsr_review = tables.Column(verbose_name='DSR SLA', accessor='dsrconclusions.final_breach_conclusion')

我最终得到: django-tables2

我被困住了。我究竟做错了什么?!这有可能吗?

环境

django.db.utils.OperationalError: (1054, "Unknown column 'normalized_data.conclusion_id' in 'field list'")

0 个答案:

没有答案