2016-11-30 25 views
4

我有兩個數據庫定義,default這是一個普通的MySQL後端和redshift(使用postgres後端)。我想使用RedShift作爲僅用於django-sql-explorer的只讀數據庫。使用RedShift作爲額外的Django數據庫

這是我在my_project/common/routers.py創建的路由器:

class CustomRouter(object): 
    def db_for_read(self, model, **hints): 
     return 'default' 

    def db_for_write(self, model, **hints): 
     return 'default' 

    def allow_relation(self, obj1, obj2, **hints): 
     db_list = ('default',) 
     if obj1._state.db in db_list and obj2._state.db in db_list: 
      return True 
     return None 

    def allow_migrate(self, db, app_label, model_name=None, **hints): 
     return db == 'default' 

而且我settings.py引用它像這樣:

DATABASE_ROUTERS = ['my_project.common.routers.CustomRouter', ] 

調用makemigrations時出現問題,Django的拋出一個錯誤指示它試圖在RedShift中創建django_*表(並且由於postgres類型012而顯然失敗不被紅移支持:

... 
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc) 

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (Column "django_migrations.id" has unsupported type "serial".) 

所以我的問題是雙重的:

  1. 是否可以完全禁用的Django管理的數據庫,但仍然使用ORM?
  2. 禁止只讀副本,爲什麼Django不認爲它是一個可接受的用例來支持只讀數據庫?

相關問題

- Column 'django_migrations.id' has unsupported type 'serial' [ with Amazon Redshift]

+1

@KevinChristopherHenry我很確定它發生在'makemigrations'腳本中,即使在一個新的項目中。在後端的執行調用中,'ensure_backend()'檢查是否存在遷移表,如果不存在,我相信會創建它。 –

+1

嗯,好吧,我不能說我已經足夠好了。祝你好運。 –

回答

4

我才發現,原來這是一個bug的結果。它在少數永久居民,最主要的是得到了解決:https://github.com/django/django/pull/7194

因此,要回答我自己的問題:

  1. 不,這是目前無法。最好的解決方案是將自定義數據庫路由器與只讀數據庫帳戶結合使用,並在路由器中將allow_migrate()返回False
  2. 最好的解決方案是升級到Django> = 1.10.4,而不是使用自定義數據庫路由器來阻止該錯誤。但是,如果您定義了其他任何數據庫(例如只讀副本),則這是一個警告。
相關問題