I have separate apps with a foreign key from a model in one model (the UserProfile model) to another app I've created. When I try to syncdb
I'm getting that the foreign key id is NULL even though I have a fixture for it in that app. Is there a way to specify the order in which YAML files are loaded?
Here's what the app structure looks like -
profile/
# models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
school = models.ForeignKey(School)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
# fixtures/initial_data.yaml
- fields:
username: admin
password: ***password hash here***
date_joined: 2012-02-01 01:00:00
email: [email protected]
last_name: Last
first_name: First
groups: []
is_active: true
is_staff: true
is_superuser: true
last_login: 2012-02-01 01:01:00
user_permissions: []
model: auth.user
pk: 1
- fields:
user: 1
school: 1
model: profile.UserProfile
pk: 1
school/
# models.py
class School(models.Model):
name = models.CharField(max_length=100)
# fixtures/initial_data.yaml
- fields:
name: "School"
model: school.School
pk: 1
And on syncdb
I get:
IntegrityError: profile_userprofile.school_id may not be NULL
This must mean it's saving the UserProfile model before the schools. So, I'm wondering if it's at all possible to reorder the yaml files or somehow attribute the school to my UserProfile without allowing the school to be NULL.
In settings.py I'm loading school
followed by profile
in installed apps.
Right approach here is to use natural keys.
If you can't use it, you can load fixtures manually, just rename them and load by manage.py loaddata
in the order you need.