[Fixed]-Creating multiple objects with foreign key

16👍

users = UserFactory.create_batch(10)
for user in users:
    doc = DocFactory.create(user=user)

8👍

You can use a post_generation decorator:

class UserFactory(factory.Factory):

    ...

    @factory.post_generation
    def create_docs(self, create, extracted, **kwargs):
        if not create:
            return
        for i in range(50):
            doc = DocFactory.create(user=self)

1👍

You can simply do batch_create so that for each DocFactory object new UserFactory object will create

`DocFactory.create_batch(10)`

0👍

For those of you working with SQLAlchemy, this can be done with the following recipe (notice that I’m using the Person/Address models instead of the User/Docs model example above).

from sqlalchemy import create_engine, Integer, Text, ForeignKey, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, scoped_session, sessionmaker

import factory
from factory.alchemy import SQLAlchemyModelFactory as sqla_factory
import random

engine = create_engine("sqlite:////tmp/factory_boy.sql")
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()

class Person(Base):
    id = Column(Integer, primary_key=True)
    name = Column(Text)
    addresses = relationship("Address", backref="person")

class Address(Base):
    id = Column(Integer, primary_key=True)
    city = Column(Text)
    street = Column(Text)
    street_number = Column(Integer)
    person_id = Column(Integer, ForeignKey('person.id'))

class AddressFactory(sqla_factory):
    class Meta:
        model = Address
        sqlalchemy_session = session
    street_number = random.randint(0, 10000)
    street = "Commonwealth Ave"
    city = "Boston"

class PersonFactory(sqla_factory):
    class Meta:
        model = Person
        sqlalchemy_session = session
    name = "John Doe"

Base.metadata.create_all(engine)
for i in range(100):
    person = PersonFactory(addresses=AddressFactory.create_batch(3))

This creates 3 workouts for each person created, where each workout references the person via the person_id FK.

Leave a comment