迁移django 1.11

时间:2017-08-02 05:24:02

标签: python django-models models

为什么当我尝试为我添加的每个模型迁移模型时,虽然有些错误,例如:

我不想删除并创建另一个数据库。我只想更改现有的数据库,如果我的数据库中有一些数据?

(1060, "Duplicate column name ...)
(1054, "Unknown column 'vacina_agentmodel.phone' in 'field list'")
(1054, "Unknown column 'created' in 'vacina_vaccinemodel'")

初始迁移

dependencies = [
    ('vacina', '0001_initial'),
]

operations = [
    migrations.CreateModel(
        name='PatientModel',
        fields=[
            ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
            ('complete_name', models.CharField(max_length=100, verbose_name='Nome completo')),
            ('birthday', models.DateField(verbose_name='Data de nascimento')),
            ('email', models.EmailField(default='', max_length=200, verbose_name='Email')),
            ('phone', models.IntegerField(blank=True, verbose_name='Número de telefone')),
            ('address', models.CharField(default='', max_length=200, verbose_name='Endereço')),
            ('house_number', models.CharField(blank=True, max_length=10, verbose_name='Número da casa/apartamento')),
            ('country', models.CharField(default='', max_length=100, verbose_name='Pais')),
            ('province', models.CharField(default='', max_length=100, verbose_name='Estado')),
            ('zip_code', models.IntegerField(verbose_name='CEP')),
            ('created_at', models.DateTimeField(auto_now_add=True)),
            ('last_modification', models.DateTimeField(auto_now=True)),
        ],
        options={
            'ordering': ('created_at',),
        },
    ),
]

///////////////////////

dependencies = [
    ('vacina', '0008_auto_20170802_0507'),
]

operations = [
    migrations.AlterModelOptions(
        name='agentmodel',
        options={'ordering': ('created_at',)},
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='country',
        field=models.CharField(default='', max_length=100, verbose_name='Pais'),
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='created_at',
        field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
        preserve_default=False,
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='house_number',
        field=models.CharField(blank=True, max_length=10, verbose_name='Número da casa/apartamento'),
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='last_modification',
        field=models.DateTimeField(auto_now=True),
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='phone',
        field=models.IntegerField(blank=True, default=django.utils.timezone.now, verbose_name='Número de telefone'),
        preserve_default=False,
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='province',
        field=models.CharField(default='', max_length=100, verbose_name='Estado'),
    ),
    migrations.AddField(
        model_name='agentmodel',
        name='zip_code',
        field=models.IntegerField(default=django.utils.timezone.now, verbose_name='CEP'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='agentmodel',
        name='email',
        field=models.EmailField(default='', max_length=200, verbose_name='Email'),
    ),
]

2 个答案:

答案 0 :(得分:1)

嘿,在你的app文件夹中会有一个名为migrations的文件夹尝试删除格式为001_initial.py的所有文件,然后从命令提示符运行makemigrations命令,这样就可以编辑模型而不删除整个数据库。

编辑:嘿,请关注此链接:Currently using Django "Evolution", is "South" better and worth switching?尝试使用南方也许这是您在设置数据库后处理更改的问题的答案

答案 1 :(得分:0)

我的models.py

class PatientModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
complete_name = models.CharField(_('Nome completo'), max_length=100, blank=False)
birthday = models.DateField(_('Data de nascimento'), blank=False, null=False)
email = models.EmailField(_('Email'), max_length=200, blank=False, default='')
phone = models.IntegerField(_('Número de telefone'), blank=True)
address = models.CharField(_('Endereço'), max_length=200, blank=False, default='')
house_number = models.CharField(_('Número da casa/apartamento'), max_length=10, blank=True)
country = models.CharField(_('Pais'), max_length=100, blank=False, default='')
province = models.CharField(_('Estado'), max_length=100, blank=False, default='')
zip_code = models.IntegerField(_('CEP'), blank=False)
created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
last_modification = models.DateTimeField(auto_now_add=False, auto_now=True)

class Meta: 
    ordering = ('created_at',)

def __meta__(self):
    return self.complete_name


class AgentModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
complete_name = models.CharField(_('Nome completo'), max_length=100, blank=False)
birthday = models.DateField(_('Data de nascimento'), blank=False, null=False)
email = models.EmailField(_('Email'), max_length=200, blank=False, default='')
phone = models.IntegerField(_('Número de telefone'), blank=True)
address = models.CharField(_('Endereço'), max_length=200, blank=False, default='')
house_number = models.CharField(_('Número da casa/apartamento'), max_length=10, blank=True)
country = models.CharField(_('Pais'), max_length=100, blank=False, default='')
province = models.CharField(_('Estado'), max_length=100, blank=False, default='')
zip_code = models.IntegerField(_('CEP'), blank=False)
created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
last_modification = models.DateTimeField(auto_now_add=False, auto_now=True)

class Meta: 
    ordering = ('created_at',)

def __meta__(self):
    return self.complete_name


class VaccineModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
vaccine_name = models.CharField(_('Nome da vacina'), max_length=255, blank=False)
against_indicative = models.TextField(_('Contra indicativo'), max_length=500, blank=True)
avoided_diseases = models.TextField(_('Doenças evitadas'), max_length=200, blank=False)
age = models.CharField(_('Idade'), max_length=255, blank=False)
comments = models.TextField(_('Comentário adicional'), max_length=200, blank=True)
created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
last_modification = models.DateTimeField(auto_now_add=False, auto_now=True)

class Meta:
    ordering = ('vaccine_name',)

def __str__(self):
    return self.vaccine_name