django中的多数据库:错位的表

时间:2018-05-03 11:32:25

标签: django database postgresql database-migration multiple-databases

我的项目中有两个postgres数据库,每个应用程序有一个:app1:Store,app2:Warehouse。他们两个都有订单模型,其中django名称:store_order& warehouse_order(我对那部分很好); 但是在迁移之后出现问题,store_order和warehouse_order都在warehouse_db中,而且我想要的所有这些django表只在store_db中:

warehouse_db store_db

github repo

settings.py

DATABASES = {
    'default': {},
    'store': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'store_db',
        'PORT': 5432,
    },
    'warehouse': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'warehouse_db',
        'PORT': 5432,
    }
}

DATABASE_ROUTERS = ['warehouse.router.WarehouseRouter', 'store.router.StoreRouter']

仓库/ router.py

class WarehouseRouter:
    """
    A router to control operations in warehouse app
    """
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'warehouse':
            return 'warehouse'
        else:
            return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'warehouse':
            return 'warehouse'
        else:
            return None

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

存储/ router.py

class StoreRouter:
    """
    A router to control operations in store app
    """
    def db_for_read(self, model, **hints):
        return 'store'

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

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

warehouse / models.py也与store / models.py相同,但app_label

除外
class Order(models.Model):
    id = models.CharField(max_length=128, null=False, unique=True, primary_key=True, default=uuid.uuid1)
    amount = models.IntegerField(null=False)
    price = models.FloatField(null=False)
    comment = models.TextField(null=True)
    created_at = models.DateTimeField(null=False, auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


def __str__(self):
    return f'{self.id}: {self.created_at}'

class Meta:
    app_label = 'warehouse'

我尽量接近这个example

和我的迁移脚本

#!/bin/sh -

docker-compose run crestopher python manage.py makemigrations
docker-compose run crestopher python manage.py migrate --database=warehouse    
docker-compose run crestopher python manage.py migrate --database=store

我还尝试过makemigrations并使用一个db进行迁移,然后使用其他db进行迁移,但结果是相同的。

修改

我在第二天再次运行它并获得:

enter image description here enter image description here

我不明白为什么。我将迁移脚本更改为:

#!/bin/sh -

docker-compose run crestopher python manage.py makemigrations warehouse
docker-compose run crestopher python manage.py migrate --database=warehouse

docker-compose run crestopher python manage.py makemigrations store
docker-compose run crestopher python manage.py migrate --database=store

但是在第一天我运行了大约1000次没有效果。我将尝试了解发生了什么,这些默认的django表仍然只应该存储在db中。如果我能找到答案,我会在这里给出答案。

0 个答案:

没有答案