I have two models with a onetoone relationship. I created a faker script to create data but keep getting this error on the demographic object creation:
django.db.utils.IntegrityError: UNIQUE constraint failed: demographic.patient_id
models.py
class PatientBase(models.Model):
creation_date = models.DateField(auto_now_add=True)
mrn = models.PositiveIntegerField(unique=True)
first_name = models.CharField(_('First Name'), max_length=30, null=True)
last_name = models.CharField(_('Last Name'), max_length=50, null=True)
middle_name = models.CharField(_('Middle Name'), max_length=30, null=True)
has_ssn = models.BooleanField(_('Has SSN'), null=True, blank=True)
ssn = models.CharField(_('SSN'), max_length=9, null=True)
phone_number_us = models.CharField(_('Phone Number'), max_length=12, null=True)
street_address = models.CharField(max_length=100, null=True)
city = models.CharField(max_length=30, null=True)
state = models.CharField(max_length=30, null=True, choices=choices.STATES)
zip_code = models.IntegerField(null=True)
county = models.CharField(max_length=50, null=True)
country = models.CharField(max_length=30, null=True, choices=choices.COUNTRIES)
primary_payor = models.CharField(max_length=100, choices=choices.PAYOR, null=True)
secondary_payor = models.CharField(max_length=100, choices=choices.PAYOR, null=True)
def __str__(self):
return f'{self.last_name} - {self.mrn}'
def get_absolute_url(self):
return reverse('patient-detail', kwargs={"pk": self.pk})
class Meta:
verbose_name = 'Patient'
verbose_name_plural = 'Patients'
db_table = 'patient'
class Demographic(models.Model):
patient = models.OneToOneField(PatientBase, on_delete=models.CASCADE)
date_of_birth = models.DateField(null=True)
has_race = models.BooleanField(null=True, blank=True)
race = models.CharField(max_length=100, choices=choices.RACE, null=True)
is_hispanic = models.BooleanField(null=True, blank=True)
height = models.FloatField(null=True)
height_unit = models.CharField(max_length=2)
weight = models.FloatField(null=True)
weight_unit = models.CharField(max_length=2)
gender = models.CharField(max_length=8)
@property
def age(self):
today = date.today()
return today.year - self.date_of_birth.year - ((today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day))
@property
def bmi(self):
w = self.weight
h = self.height
if self.height_unit == 'in':
h = h * 2.54
if self.weight_unit == 'lbs':
w = w * 0.453592
return w/(h*h)
def __str__(self):
return self.patient.last_name
class Meta:
verbose_name = 'Demographic'
verbose_name_plural = 'Demographics'
db_table = 'demographic'
faker script
from patient.models import PatientBase, Demographic, Medical_History, Diagnosis
from django.core.management.base import BaseCommand
from faker import Faker
from patient.choices import PAYOR, STATES, COUNTRIES, RACE, GENDER, YNU, CLD_SEVERITY
from random import randint
class Command(BaseCommand):
help = 'Generate fake data for patient app'
def handle(self, *args, **kwargs):
fake = Faker()
for _ in range(50):
patient = PatientBase.objects.create(
mrn = fake.random_int(min=100000, max=999999),
first_name=fake.first_name(),
last_name=fake.last_name(),
creation_date=fake.date_this_decade(before_today=True, after_today=False),
street_address=fake.address(),
city=fake.random_element(elements=['Pittsburgh', 'Cranberry', 'Butler',
'Beaver', 'Washington', 'Greensburg',
'Monroeville', 'Wexford', 'Sewickley']),
state=fake.random_element(elements=['PA', 'OH', 'WV']),
county=fake.random_element(elements=['Allegheny', 'Butler', 'Beaver', 'Washington', 'Westmoreland']),
zip_code=fake.zipcode(),
primary_payor=fake.random_element(PAYOR),
secondary_payor=fake.random_element(PAYOR)
)
Demographic.objects.create(
patient = patient,
date_of_birth = fake.date_of_birth(minimum_age=18, maximum_age=99),
has_race = fake.boolean(chance_of_getting_true=50),
race = fake.random_choices(elements=RACE),
is_hispanic = fake.boolean(chance_of_getting_true=50),
height = fake.pydecimal(left_digits=3, right_digits=1, positive=True, max_value=175, min_value=75),
weight = fake.pydecimal(left_digits=3, right_digits=1, positive=True, max_value=200, min_value=50),
weight_unit = 'kg',
height_unit = 'cm',
gender = fake.random_choices(elements=GENDER)
)