【问题标题】:Make migrations using models corresponding to the different databases in Django使用对应于 Django 中不同数据库的模型进行迁移
【发布时间】:2019-01-19 22:25:28
【问题描述】:

这是我在 app 中的 models.py 文件。

从 django.db 导入模型

class ImaraInventory(models.Model):
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    day = models.DateField()
    partner_id = models.CharField(max_length=255)
    qty = models.BigIntegerField()
    sku_id = models.CharField(max_length=255)
    is_virtual = models.IntegerField()

    class Meta:
        unique_together = ('channel_type', 'branch', 'partner_id', 'day', 'sku_id')



class ImaraSales(models.Model):
    attribution = models.CharField(max_length=255)
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    day = models.DateField()
    partner_id = models.CharField(max_length=255)
    sku_id  = models.CharField(max_length=255)
    live = models.BooleanField()
    disc_value = models.DecimalField(decimal_places=3,max_digits=12)
    revenue = models.DecimalField(decimal_places=3,max_digits=12)
    sales_qty = models.IntegerField()

    class Meta:
        unique_together = ('attribution', 'branch' , 'channel_type', 'day', 'partner_id', 'sku_id')


class ImaraReturns(models.Model):
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    partner_id = models.CharField(max_length=255)
    sku_id = models.CharField(max_length=255)
    day = models.DateField()
    return_qty = models.IntegerField()

    class Meta:
        unique_together =  ('branch', 'channel_type', 'partner_id', 'sku_id', 'day')



class WrognInventory(models.Model):
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    day = models.DateField()
    partner_id = models.CharField(max_length=255)
    qty = models.BigIntegerField()
    sku_id = models.CharField(max_length=255)
    is_virtual = models.IntegerField()

    class Meta:
        unique_together = ('channel_type', 'branch', 'partner_id', 'day', 'sku_id')



class WrognSales(models.Model):
    attribution = models.CharField(max_length=255)
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    day = models.DateField()
    partner_id = models.CharField(max_length=255)
    sku_id  = models.CharField(max_length=255)
    live = models.BooleanField()
    disc_value = models.DecimalField(decimal_places=3,max_digits=12)
    revenue = models.DecimalField(decimal_places=3,max_digits=12)
    sales_qty = models.IntegerField()

    class Meta:
        unique_together = ('attribution', 'branch' , 'channel_type', 'day', 'partner_id', 'sku_id')



class WrognReturns(models.Model):
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    partner_id = models.CharField(max_length=255)
    sku_id = models.CharField(max_length=255)
    day = models.DateField()
    return_qty = models.IntegerField()

    class Meta:
        unique_together =  ('branch', 'channel_type', 'partner_id', 'sku_id', 'day')



class MstakenInventory(models.Model):
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    day = models.DateField()
    partner_id = models.CharField(max_length=255)
    qty = models.BigIntegerField()
    sku_id = models.CharField(max_length=255)
    is_virtual = models.IntegerField()

    class Meta:
        unique_together = ('channel_type', 'branch', 'partner_id', 'day', 'sku_id')


class MstakenSales(models.Model):
    attribution = models.CharField(max_length=255)
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    day = models.DateField()
    partner_id = models.CharField(max_length=255)
    sku_id  = models.CharField(max_length=255)
    live = models.BooleanField()
    disc_value = models.DecimalField(decimal_places=3,max_digits=12)
    revenue = models.DecimalField(decimal_places=3,max_digits=12)
    sales_qty = models.IntegerField()

    class Meta:
        unique_together = ('attribution', 'branch' , 'channel_type', 'day', 'partner_id', 'sku_id')



class MstakenReturns(models.Model):
    branch = models.CharField(max_length=255)
    channel_type = models.CharField(max_length=255)
    partner_id = models.CharField(max_length=255)
    sku_id = models.CharField(max_length=255)
    day = models.DateField()
    return_qty = models.IntegerField()

    class Meta:
        unique_together =  ('branch', 'channel_type', 'partner_id', 'sku_id', 'day')

这是我在应用程序中的 dbrouters.py 文件。

from etl.models import WrognSales,WrognInventory, WrognReturns
from etl.models import ImaraSales, ImaraInventory, ImaraReturns
from etl.models import MstakenSales, MstakenInventory, MstakenReturns

class WrognDBRouter(object):

    def db_for_read(self,model,**hints):
        if model == WrognSales or model == WrognInventory or model == WrognReturns:
            return 'db_usplwrogn'

    def db_for_write(self,model,**hints):
        if model == WrognSales or model == WrognInventory or model == WrognReturns:
            return 'db_usplwrogn'



class ImaraDBRouter(object):

    def db_for_read(self,model,**hints):
        if model == ImaraInventory or model == ImaraReturns or model == ImaraSales:
            return 'db_usplimara'

    def db_for_write(self,model,**hints):
        if model == ImaraInventory or model == ImaraReturns or model == ImaraSales:
            return 'db_usplimara'


class MstakenDBRouter(object):

    def db_for_read(self,model,**hints):
        if model == MstakenInventory or model == MstakenReturns or model == MstakenSales:
            return 'db_usplmstaken'

    def db_for_write(self,model,**hints):
        if model == MstakenInventory or model == MstakenReturns or model == MstakenSales:
            return 'db_usplmstaken'

这些都是settings.py文件中的数据库。

    DATABASES = {
    'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'etlui',
      'HOST': 'localhost',
      'USER': '***',
      'PASSWORD': '***',
  },
  'db_usplimara':{
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'usplimara',
      'HOST': 'localhost',
      'USER': '***',
      'PASSWORD': '***',
  },
  'db_usplmstaken':{
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'usplmstaken',
      'HOST': 'localhost',
      'USER': '***',
      'PASSWORD': '***',
  },
  'db_usplwrogn':{
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'usplwrogn',
      'HOST': 'localhost',
      'USER': '**',
      'PASSWORD': '***',
  }

}

我想根据每个客户端的模型迁移到不同的数据库。我执行了命令“python manage.py makemigrations app_name”,然后对每个数据库使用此命令“python manage.py migrate --database=db_usplmstaken”进行迁移。但它在所有三个数据库中创建了所有九个模型。 请告诉我如何根据每个数据库对应的模型进行迁移。

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    Django 中迁移多个数据库很简单,但也是多余的:migrate 命令一次只对一个数据库进行操作,因此您必须为每个数据库单独运行它。

    要迁移第二个数据库,您只需运行python manage.py migrate --database=example_db

    这在生产环境中很好,当您不经常迁移时,但在本地环境的积极开发过程中,它可能会变得很累。为了解决这个问题,我们使用Fabric 来简化复杂的任务。在这个特定的例子中,我们有一个命令在我们的两个开发数据库上运行迁移。

    您也可以访问以下链接了解更多说明: https://kite.com/python/docs/fabric

    【讨论】:

      【解决方案2】:

      重要的不是db_for_read和db_for_write,当你使用migrate命令时,重要的是allow_migrate函数。

      例如:

      class WrognDBRouter(object):
      
          def allow_migrate(self, db, app_label, model_name=None, **hints):
              if model_name in ['WrognSales', 'WrognInventory', 'WrognReturns'] and db == 'db_usplwrogn':
                  return True
      

      您选择模型名称和数据库并允许迁移。

      更多信息:https://docs.djangoproject.com/en/2.2/topics/db/multi-db/#allow_migrate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-04
        • 1970-01-01
        • 2014-08-06
        • 2021-05-01
        • 2017-08-19
        • 2021-09-11
        • 2014-01-29
        • 2019-06-02
        相关资源
        最近更新 更多