2016-12-12 38 views
2

在此先感謝您的幫助。在mi項目中,我有一個應用程序,涉及從現有數據庫生成的模型。由於這些表由DBA管理,因此它們保留爲非託管模式。由於模式的變化,我們可能需要從db重新生成模型,因此我們爲每個模型創建了替代代理模型,以將我們管理的部分與我們不管理的部分分開。在下面你可以看到一個基於我們當前佈局的例子。Django 1.10 - makemigrations命令沒有檢測到非託管模型的變化

該示例顯示了帶有FK的生成模型與另一個生成的模型,因此該代理模型具有對非代理模型的引用。我已閱讀指出here的討論,並嘗試了一些顯示的方法,但是他們中沒有人爲我工作。所以現在我試圖更新生成的模型,指向代理服務器,我認爲這不應該導致任何問題。

正如我所看到的,Django爲非託管模型生成了遷移,我認爲makemigration會檢測該模型的FK中的變化。但是,當我運行manage.py makemigrations它顯示沒有檢測到更改。 對於非託管模型,這是預期的makemigrations行爲嗎?

# app/models.py 
class SacLocation(models.Model): 
    sacloc_location_id = models.IntegerField(primary_key=True) 
    sacloc_name = models.CharField(max_length=50, blank=True, null=True) 
    sacloc_state = models.IntegerField(blank=True, null=True) 

    # I'm changing this Field to point to the proxy model 
    # e.g. it will look like this, but the change is not detected by makemigrations 
    # sacloc_location_grouping = models.ForeignKey('LocationGroupingProxy', 
    #   models.DO_NOTHING, db_column='sacloc_location_grouping') 
    sacloc_location_grouping = models.ForeignKey('SacLocationGrouping', 
       models.DO_NOTHING, db_column='sacloc_location_grouping') 

    class Meta: 
     managed = False 
     db_table = 'sac_location' 


class SacLocationGrouping(models.Model): 
    saclgr_location_grouping__id = models.IntegerField(primary_key=True) 
    saclgr_name = models.CharField(max_length=50, blank=True, null=True) 

    class Meta: 
     managed = False 
     db_table = 'sac_location_grouping' 


class LocationProxy(SacLocation):   
    class Meta: 
     proxy = True 

    def __str__(self): 
     return u'%s' % (self.sacloc_name) 


class LocationGroupingProxy(SacLocationGrouping): 
    class Meta: 
     proxy = True 

    def __str__(self): 
     return u'%s' % (self.saclgr_name) 

回答

0

我已經在我的代碼做了一些改動,使其指向非託管模式,它最初FK到其他非託管模式,以代理模式。這些變化都沒有導致產生新的遷移,所以我認爲在這種情況下預期的行爲就是這樣。看着Django的源代碼,但未能發現檢測到這些變化的地方。最後,當我更改代理模型中的Meta選項(例如排序)時,Django實際上檢測到了這些更改並創建了新的遷移。

相關問題