It doesn't seem like its an issue with pytest, as you are facing this with normal migrations too. Can we see your migration/data migration file? Generally a data migration would come after the table set up.
import django.db.models.deletion
from django.db import migrations, models
def populate_document_owners(apps, schema_editor):
Document = apps.get_model('some_schema', 'Document')
User = apps.get_model('some_schema', 'User')
owner_of_all_docs = User.objects.first()
for d in Document.objects.all():
d.user = owner_of_all_docs
d.save()
class Migration(migrations.Migration):
dependencies = [
('some_schema', '0100_previous_migration'),
]
operations = [
migrations.AddField(
model_name='document',
name='owner',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='documents',
to='some_schema.user',
),
),
migrations.RunPython(populate_document_owners),
]