I have the need to create many EVENT objects at once and subsequently create many ARCHIVED_EVENT objects that have a foreign key to their corresponding event.
My code looks like something like this:
events = []
archivedEvents = []
for _ in range(1000):
event = Event(name="Test")
archivedEvent = ArchivedEvent(event_id=event.id)
archivedEvents.append(archivedEvent)
events.append(event)
Event.objects.bulk_create(events)
ArchivedEvent.objects.bulk_create(archivedEvents)
Unfortunately all Archived Events created here have a NULL foreign key to an EVENT. I understand that the primary key for an object is not generated until it is saved to the database. But I save the events before creating the archived events. Am I missing something? Should I refresh the cache before bulk creating the archived events?