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

时间:2017-10-10 18:52:37

标签: python django django-models migration django-migrations

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

3 个答案:

答案 0 :(得分:10)

choice参数应为确定性可迭代,如列表或元组

set 在Python 3.3+中随机化,不能作为选择

更改

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

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

答案 1 :(得分:0)

最近,我遇到了这个问题,发现解决方案有点奇怪,即使我不知道问题的根本原因。

我在同一model.py中具有以下模型:

class Status(models.Model):
    status=models.CharField(db_column='status',max_length=50, null=False, blank=False)


class Details(models.Model):
    #notes_names=models.CharField(max_length=50,db_column='notes_names')
    #file=models.FileField(upload_to='notes/%Y/%m/%d',max_length=255,null=True,blank=True)
    #user = models.ForeignKey(User,on_delete=models.CASCADE)
    faculty = models.ForeignKey(Registration,db_column='faculty_id',on_delete=models.CASCADE)
    degree=models.ForeignKey(Notes_Degree,on_delete=models.CASCADE)
    seqNo=models.IntegerField(default=1)
    branch=models.ForeignKey(Notes_Branch,on_delete=models.CASCADE)
    semester=models.ForeignKey(Notes_Semester,on_delete=models.CASCADE)
    subject=models.ForeignKey(Notes_Subject,on_delete=models.CASCADE)
    file=models.FileField(validators=[FileExtensionValidator(allowed_extensions=['pdf'])])
    fileType= models.CharField(db_column='file_type',max_length=30, null=True, blank=True)
    size= models.FloatField(default=0)
    url=models.CharField(db_column='url',max_length=1000, null=True, blank=True)
    pages=models.BigIntegerField(null=False, blank=False)
    # Try later ContentTypeRestrictedFileField(upload_to='uploads/', content_types=['video/x-msvideo', 'application/pdf', 'video/mp4', 'audio/mpeg', ],max_upload_size=5242880,blank=True, null=True)
    #filename= models.CharField(db_column='file_name',max_length=80, null=False, blank=False)
    #sample notes columns
    sampleFile=models.FileField(db_column='sample_file',validators=[FileExtensionValidator(allowed_extensions=['pdf'])])
    sampleSize=models.FloatField(default=0)
    sampleFileType= models.CharField(db_column='sample_file_type',max_length=30, null=True, blank=True)
    sampleUrl=models.CharField(db_column='sample_url',max_length=1000, null=True, blank=True)
    updateSeqNo=models.IntegerField(default=0)
    uploadedAt= models.DateTimeField(db_column='uploaded_date',auto_now_add=False,null=False, blank=False)
    updatedAt= models.DateTimeField(db_column='updated_date',auto_now_add=False,null=False, blank=False)
    active=models.BooleanField(default=True)
    status=models.ForeignKey(Status,on_delete=models.CASCADE,default=1)
    description=models.CharField(db_column='description',max_length=100, null=True, blank=True)
    """
    #url=models.TextField(blank=True, null=True),
    userId/notes/degree/Branch/Semester/SubjectId/Seqno/NotesId/updatedTime/Filename
    """

在这种情况下,每次我进行迁移时,都是在现场“状态”中创建auto_migration.py类。

为解决此问题,我将Status类添加到另一个模块的model.py中并导入。它正常工作并解决了。

我真的不确定为什么,因为在同一个项目中,上述类型的结构运行良好。

答案 2 :(得分:-4)

在进行新迁移之前,您必须先迁移

  

./ mange.py migrate

否则,它将继续与当前数据库进行比较并创建新文件。