djangouuiddjango-fixtures

Django: OnetoOne relations using fixtures and uuids


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?


Solution

  • Ok, solution found! Instead of using idjust use pk