编程语言
首页 > 编程语言> > python – Django迁移:使用makemigrations创建相同的迁移

python – Django迁移:使用makemigrations创建相同的迁移

作者:互联网

Django在调用时重复创建相同的迁移文件:

./manage.py makemigrations

无论是否迁移更改,每次运行makemigrations时,都会在新的迁移文件中创建相同的迁移.

过程如下:

./manage.py makemigrations app

Migrations for 'app':
project/app/migrations/0007_auto_20171010_1837.py
- Alter field charge_type on charge
- Alter field fee_type on fee
- Alter field event_type on orderevent


./manage.py migrate app

Running migrations:
Applying mws.0007_auto_20171010_1837... OK


./manage.py makemigrations app

Migrations for 'app':
project/app/migrations/0008_auto_20171010_1838.py
- Alter field charge_type on charge
- Alter field fee_type on fee
- Alter field event_type on orderevent


./manage.py makemigrations app

Migrations for 'app':
project/app/migrations/0009_auto_20171010_1839.py
- Alter field charge_type on charge
- Alter field fee_type on fee
- Alter field event_type on orderevent

我很好奇为什么在makemigrations和migrate命令之间没有对模型进行任何更改时,为什么继续使用更新的迁移文件名创建新的相同迁移.

模型看起来像这样:

目前的应用模型:

class OrderEvent(models.Model):
    client = models.ForeignKey('clients.Client')

    SHIPMENT_EVENT = 'she'
    REFUND_EVENT = 'ree'
    CHARGEBACK_EVENT = 'cbe'
    GUARANTEE_CLAIM_EVENT = 'gce'

    EVENT_TYPE_CHOICES = {
        (SHIPMENT_EVENT, 'Shipment Event'),
        (REFUND_EVENT, 'Refund Event'),
        (CHARGEBACK_EVENT, 'Chargeback Event'),
        (GUARANTEE_CLAIM_EVENT, 'Guarantee Claim Event'),
    }

    event_type = models.CharField(max_length=3, choices=EVENT_TYPE_CHOICES)
    amazon_order_id = models.CharField(max_length=19)
    seller_order_id = models.CharField(max_length=19)
    marketplace_name = models.CharField(max_length=14)
    posted_date = models.DateTimeField(blank=True, null=True)


class ShipmentItem(models.Model):
    order_event = models.ForeignKey('OrderEvent')
    seller_sku = models.CharField(max_length=128)
    order_item_id = models.CharField(max_length=19)
    quantity_shipped = models.IntegerField()



class Charge(models.Model):
    shipment_item = models.ForeignKey('ShipmentItem', blank=True, null=True)

    PAYMENT_METHOD_FEE = 'pmf'
    EXPORT_CHARGE = 'exc'
    SAFET_REIMBURSEMENT = 'str'
    OTHER = 'oth'

    CHARGE_TYPE_CHOICES = {
        (PAYMENT_METHOD_FEE, 'Payment Method Fee'),
        (EXPORT_CHARGE, 'Export Charge'),
        (SAFET_REIMBURSEMENT, 'SAFET Reimbursement'),
        (OTHER, 'Other'),
    }
    charge_type = models.CharField(
        max_length=3,
        choices=CHARGE_TYPE_CHOICES,
        blank=True,
        null=True
    )
    charge_currency_code = models.CharField(
        max_length=3,
        blank=True,
        null=True
    )
    charge_amount = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        blank=True,
        null=True
    )


class Fee(models.Model):
    shipment_item = models.ForeignKey('ShipmentItem', blank=True, null=True)
    TAPING_FEE = 'taf'
    TRANSPORTATION_FEE = 'trf'
    OTHER = 'oth'
    FEE_TYPE_CHOICES = {
        (TAPING_FEE, 'Taping Fee'),
        (TRANSPORTATION_FEE, 'Transportation Fee'),
        (OTHER, 'Other'),
    }
    fee_type = models.CharField(
        max_length=3,
        choices=FEE_TYPE_CHOICES,
        blank=True,
        null=True
    )
    fee_currency_code = models.CharField(
        max_length=3,
        blank=True,
        null=True
    )
    fee_amount = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        blank=True,
        null=True
    )

客户端应用模型:

class Client(models.Model):

    name = models.CharField(max_length=128)
    code = models.CharField(
        max_length=16,
        blank=True,
        unique=True,
        help_text="Example: 00042",
    )
    slug = AutoSlugField(max_length=128, unique=True, populate_from='name')

    INVOICE_LEVEL_PARENT = 'pa'
    INVOICE_LEVEL_CHILD = 'ch'

    INVOICE_LEVEL_CHOICES = {
        (INVOICE_LEVEL_PARENT, 'Parent-level Invoice'),
        (INVOICE_LEVEL_CHILD, 'Child-level Invoice'),
    }

    invoice_level = models.CharField(
        max_length=2,
        choices=INVOICE_LEVEL_CHOICES,
        default=INVOICE_LEVEL_PARENT,
    )
    payment_terms = models.CharField(max_length=30, default='Net 15')
    late_fees = models.DecimalField(default='1.50', max_digits=5, decimal_places=2)

    notes = models.TextField(blank=True)

    def __str__(self):
        return self.name

解决方法:

choice参数应该是像列表或元组一样的确定性迭代.

集合在Python 3.3中是随机的,它不是一个选择.

更改

EVENT_TYPE_CHOICES = {
    (SHIPMENT_EVENT, 'Shipment Event'), ...
}

EVENT_TYPE_CHOICES = (
    (SHIPMENT_EVENT, 'Shipment Event'), ...
)

标签:python,migration,django,django-models,django-migrations
来源: https://codeday.me/bug/20190608/1195934.html