首页 > 解决方案 > 如何在 Django ORM 中创建 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',
                ]
            )
        ]

in models.py,DsrConclusions在之前进来NormalizedData

由于有 2 个数据库,我使用数据库路由器,如下所示:

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,这是因为它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'")

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

环境

Package Name    Version
Django      2.2
Python      3.7.2
Crispy_Forms    1.7.2
Debug Toolbar   1.11
Django_Tables2  2.0.6
Django_Tables2_Column_Shifter   0.5.2

标签: djangodjango-models

解决方案


推荐阅读