Djangoã®MasterSlaveRouterã®è©±ã(稲å·æ·³äºé¢¨)
ããã¯æè¿Djangoã使ãå§ãã人ã®è©±ããªãã§ããã©ããDjangoã¯è¤æ°DBãæ±ãããã§ããããããããã§Master/Slaveãªã¬ããªã±ã¼ã·ã§ã³ãæ±ããããªã¼ãªãã¦æã£ã¦ããã§ãã
ã¨ãããããããã¥ã¡ã³ãã«æ¸ãã¦ããMasterSlaveRouterã使ã£ãæã«ä¸æè°ãªç¾è±¡ã«ééãã¡ãã£ããã§ããã
https://docs.djangoproject.com/en/dev/topics/db/multi-db/
ããã¥ã¡ã³ãã«æ¸ãã¦ããã¨ãããMasterSlaveRouterããå®è£ ãã¦ããããããsyncdbãã¦ããã¼ã¨è¦ãããããã¨åºããåºããã§ããï¼ã¨ã©ã¼
$ python manage.py syncdb . . django.db.utils.IntegrityError: (1062, "Duplicate entry 'contenttypes-contenttype' for key 'app_label'")
ããã¼ããããããªã¼ããããããªã¼ã ãªãã©syncdbãç´ãã¦ããã¼ã¨åºããªã¼ãã¦ãã°ããæãã¦åããªãã£ããã§ããã
ãã®ã¾ã¾ãããããªããªã¼ããªãã¨ãããªãã¡ããªã¼ãã¦å¼·ã念ãããããªãã¨ãåããããã«ãªã£ã¦ãä¾ã®å çã«é ¼ã£ãããåºã¦æ¥ããã§ãããä¸è¨ã®URLãã
ã©ãããããã®ç¾è±¡ã¯ãdjangoãããã©ã«ãä½ãã¢ãã«ã®ä¸ã«ãç¬èªã®ãã£ãã·ã¥æ©æ§ãåãããManagerãªãã¸ã§ã¯ãã使ã£ã¦ãã£ã¦ãããåå ã½ããªã¼ã¦ã®ãåãã£ããã§ãããããæ°ãä»ãã ã£ããã§ãããæããªã¼æããªã¼ã
ãç¥ãã®æ¹æ³ãæ¸ãã¦ãã£ã¦ãMasterSlaveRouterã®db_for_readã§ããããã£ãç¬èªã®ãã£ãã·ã¥æ©æ§ãæã£ã¦ã奴ãã¯ãå¿ ãMasterãè¦ãæ§ã«ãã¦ããã°ããã®ç¾è±¡ã¯åã¾ãããããã§ããã¼ã
def db_for_read(self, model, **hints): if model._meta.app_label in ('contenttypes', 'sites'): return 'master' else: return 'slave'
ããã¼ãããã§å®å¿ãã¦ç ãããªã¼ã¦ãè¸ãæ«ã§ä¸ããããã§ããã©ãããèããã¨ããã£ã¦å¾®å¦ãªãç¥ãã®ä»æ¹ã ãªã¼ã¦æã£ã¦ãã¾ãã¨ãã¾ããã
ãªããä»ã«è¯ãæ¹æ³ãªãã®ããªã¼ã£ã¦ä»ã§ããã®äººã¯å¤ãªå¤ãªæ¢ãã¦ããããã§ãããã
ããããªãã ãããã