将变更迁移到数据库时遇到麻烦-Python / Django

时间:2020-05-07 07:01:00

标签: django sqlite

我正在对项目中的数据库进行一些更改,但是我遇到了一些问题,它似乎不是我的一张表中的一列,但是确实存在,不知道是什么原因引起的,我不确定是必须与onetoonefield关系建立关系还是将其完全破坏了。

模型

from django.db import models

# Create your models here.

class BloodType(models.Model):
    Tipo = models.CharField(max_length=100)

    def __str__(self):
        return self.Tipo


class Gender(models.Model):
    Genero = models.CharField(max_length=100)

    def __str__(self):
        return self.Genero

class Nationality(models.Model):
    Nacionalidad = models.CharField(max_length=100)

    def __str__(self):
        return self.Nacionalidad

class CivilStatus(models.Model):
    Estado = models.CharField(max_length=100)

    def __str__(self):
        return self.Estado

class InsuranceCompany(models.Model):
    Compania = models.CharField(max_length=200)

    def __str__(self):
        return self.Compania

class Policy(models.Model):
    Tipo_Poliza = models.CharField(max_length=256)

    def __str__(self):
        return self.Tipo_Poliza

class Insurance(models.Model):
    Aseguradora = models.OneToOneField(InsuranceCompany,on_delete=models.DO_NOTHING)
    Tipo_de_Poliza =  models.OneToOneField(Policy, on_delete=models.DO_NOTHING)
    Numero_de_poliza = models.CharField(max_length=100)
    Vencimiento = models.DateField()

    def __str__(self):
        return str(self.Aseguradora) + ' ' + str(self.Tipo_de_Poliza)

class Patient(models.Model):
    Codigo = models.CharField(max_length=15)
    Nombre = models.CharField(max_length=100)
    Apellidos = models.CharField(max_length=200)
    Fecha_Nacimiento = models.DateField()
    Telefono = models.CharField(max_length=25,blank=True)
    Correo = models.EmailField(blank=True)
    Expediente = models.CharField(max_length=200, blank=True)
    Tipo_sangre = models.OneToOneField(BloodType,on_delete=models.DO_NOTHING)
    Peso = models.PositiveIntegerField()
    Estatura = models.PositiveIntegerField()
    Genero = models.OneToOneField(Gender,on_delete=models.DO_NOTHING)
    Nacionalidad = models.OneToOneField(Nationality,on_delete=models.DO_NOTHING)
    Estado_civil = models.OneToOneField(CivilStatus,on_delete=models.DO_NOTHING)
    Ocupacion = models.CharField(max_length=100,blank=True)
    Lugar_de_procedencia = models.CharField(max_length=100)
    Lugar_de_residencia = models.CharField(max_length=100)
    Seguro = models.OneToOneField(Insurance,on_delete=models.DO_NOTHING,default='No tiene seguro.')

    def __str__(self):
        return self.Nombre + ' ' + self.Apellidos

错误

OperationalError at /admin/patients/patient/
no such column: patients_patient.Seguro_id
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/patients/patient/
Django Version: 2.2.12
Exception Type: OperationalError
Exception Value:    
no such column: patients_patient.Seguro_id
Exception Location: /Users/lralcocer/PycharmProjects/MedTracker/venv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py in execute, line 383
Python Executable:  /Users/lralcocer/PycharmProjects/MedTracker/venv/bin/python
Python Version: 3.5.0
Python Path:    
['/Users/lralcocer/PycharmProjects/MedTracker/medtracker',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
 '/Users/lralcocer/PycharmProjects/MedTracker/venv/lib/python3.5/site-packages']
Server time:    Thu, 7 May 2020 06:53:16 +0000

命令错误

 #Make Migrations
(venv) MacBook-Pro:medtracker lralcocer$ python manage.py makemigrations patients
No changes detected in app 'patients'

#Python manage.py migrate
ValueError: invalid literal for int() with base 10: 'UNDEFINED'

1 个答案:

答案 0 :(得分:1)

通常Django无法捕捉到您已经更改了字段的类型(例如,当您更改主键时,外键将保留旧的类型)。我没有最好的解决方案,但是如果您在该领域没有重要数据,它应该可以工作:

  1. 从模型中删除字段。
  2. 删除上一个迁移文件(导致错误)
  3. 进行迁移,进行迁移
  4. 将已删除的字段添加到模型中
  5. 进行迁移,然后再次迁移