代码之家  ›  专栏  ›  技术社区  ›  FTM

migrate命令在第二个db-django上创建所有表

  •  0
  • FTM  · 技术社区  · 7 年前

    我有一个应用程序( ali )关于我的项目( website )我希望它有自己的数据库。问题是,当我跑步时 python manage.py migrate --database=ali ,该命令重新创建 阿里 数据库;而预期结果将只有 ali_search 数据库。

    附笔。: 应用程序似乎按预期运行 在我做了一些测试之后。换句话说,我的模型 阿里 应用程序正在保存在 阿里 数据库。不过,在我的房间里有这么多空桌子 阿里 DB不是正确的方式。

    设置:

    # website.settings
    
    ...
    
    INSTALLED_APPS = [
        'base.apps.BaseConfig',
        'ali.apps.AliConfig',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.sites',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sitemaps',
        'django_comments',
        'mptt',
        'tagging',
        'zinnia',
    ]
    
    ....
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'website',
            'USER': 'website',
            'PASSWORD': 'website',
            'HOST': 'localhost',
            'PORT': '5432',
        },
        'ali': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'ali',
            'USER': 'ali',
            'PASSWORD': 'ali',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }
    
    DATABASE_ROUTERS = [
        'ali.routers.AliRouter',
    ]
    
    ....
    

    路由器:

    # ali.routers
    
    class AliRouter:
        def db_for_read(self, model, **hints):
            if model._meta.app_label == 'ali':
                return 'ali'
            return None
    
        def db_for_write(self, model, **hints):
            if model._meta.app_label == 'ali':
                return 'ali'
            return None
    
        def allow_relation(self, obj1, obj2, **hints):
            if obj1._meta.app_label == 'ali' or \
               obj2._meta.app_label == 'ali':
               return True
            return None
    
        def allow_migrate(self, db, app_label, model_name=None, **hints):
            if app_label == 'ali':
                return db == 'ali'
            return None
    

    模型:

    # ali.models
    
    from django.db import models
    
    class Search(models.Model):
    
        results = models.IntegerField()
    

    这是我通过查询我的阿里数据库得到的 \dt :

    ali=# \dt
                      List of relations
     Schema |            Name            | Type  | Owner 
    --------+----------------------------+-------+-------
     public | ali_search                 | table | ali
     public | auth_group                 | table | ali
     public | auth_group_permissions     | table | ali
     public | auth_permission            | table | ali
     public | auth_user                  | table | ali
     public | auth_user_groups           | table | ali
     public | auth_user_user_permissions | table | ali
     public | django_admin_log           | table | ali
     public | django_comment_flags       | table | ali
     public | django_comments            | table | ali
     public | django_content_type        | table | ali
     public | django_migrations          | table | ali
     public | django_session             | table | ali
     public | django_site                | table | ali
     public | tagging_tag                | table | ali
     public | tagging_taggeditem         | table | ali
     public | zinnia_category            | table | ali
     public | zinnia_entry               | table | ali
     public | zinnia_entry_authors       | table | ali
     public | zinnia_entry_categories    | table | ali
     public | zinnia_entry_related       | table | ali
     public | zinnia_entry_sites         | table | ali
    (22 rows)
    

    但我真正期望的是:

    ali=# \dt
                      List of relations
     Schema |            Name            | Type  | Owner 
    --------+----------------------------+-------+-------
     public | ali_search                 | table | ali
    (1 row)
    

    如果应用程序与数据库同名(在本例中, 阿里 )?

    2 回复  |  直到 7 年前
        1
  •  2
  •   knbk    7 年前

    返回 None allow_migrate() 表示路由器对当前操作没有意见。如果所有配置的路由器都没有意见,则默认情况下允许该操作。您当前的路由器返回 没有 适用于除 ali ,因此允许在两个数据库上执行这些操作。

    禁止迁移 阿里 数据库,必须显式返回 False 在这些情况下,例如:

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

    现在你可以跑了 migrate 对于每个数据库,不必指定要迁移的应用程序:

    $ python manage.py migrate --database=default
    $ python manage.py migrate --database=ali
    
        2
  •  0
  •   FTM    7 年前

    对我来说 migrate 命令应该查看路由以创建表。但似乎情况并非如此。因此,最简单的解决办法似乎是 迁移 [app_label] 争论。这样地:

    python manage.py migrate ali --database=ali