1

I have a model called user, and another one called patient as follow:

class Patient(Model):
    user = OneToOneField('users.User', on_delete=CASCADE, related_name="patient",
                          blank=True, null=True)

class User(Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

All the migrations work correctly and now I'm trying to add some data to the DB using fixtures. Users fixtures work well, but it seems that Patient fixtures do not detect the UUID's from users:

django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table "patients_patient" violates foreign key constraint "patients_patient_user_id_b53513b7_fk_users_user_id"
 DETAIL:  Key (user_id)=(97179680-7042-11ea-bc55-0242ac130003) is not present in table "users_user".

Here is how my fixture looks:

Patient fixture

[{
    "model": "patients.patient",
    "id": 1,
    "fields": {
      "user": "97179680-7042-11ea-bc55-0242ac130003"
    }
  },
]

User fixture

[
    {
      "model": "users.User",
      "id": "97179680-7042-11ea-bc55-0242ac130003",
      }
    },
]

How can I solve that?

Alvaro
  • 1,430
  • 2
  • 23
  • 41
  • try this https://stackoverflow.com/questions/19857406/django-unable-to-load-test-fixtures-integrityerror/19857905#19857905 – shafik Mar 28 '20 at 09:53
  • doesn't seem to solve the problem... – Alvaro Mar 28 '20 at 10:19
  • I have checked in the db and it seems that django is generating a new uuid regardless of the pk i set in the fixture. Still I have no idea how to avoid that – Alvaro Mar 28 '20 at 10:32

1 Answers1

0

Ok, solution found! Instead of using idjust use pk

Alvaro
  • 1,430
  • 2
  • 23
  • 41