sandorlev
sandorlev

Reputation: 33

Factory Boy model with one-to-many and many-to-many fields

I'm testing my SQLAlchemy models with pytest and Factory Boy, but I find their documentation lacking in terms of relationships. I have my schema set up so there are users who can belong to multiple groups (groups can hold multiple users) and they can have multiple tokens, but a token only belongs to a single user:

_user_groups_table = Table(
    'user_groups', Base.metadata,
    Column('user_id', INTEGER(unsigned=True), ForeignKey('user.id')),
    Column('group_id', INTEGER(unsigned=True), ForeignKey('user_group.id'))
)


class UserGroup(Base):
    __tablename__ = 'user_group'

    id = Column(INTEGER(unsigned=True), Sequence('user_group_id_seq'), primary_key=True, autoincrement=True)
    name = Column(String(255), unique=True, nullable=False)


class User(Base):
    __tablename__ = 'user'

    id = Column(INTEGER(unsigned=True), Sequence('user_id_seq'), primary_key=True, autoincrement=True)
    name = Column(String(255), unique=True, nullable=False)
    groups = relationship('UserGroup', secondary=_user_groups_table)
    auth_tokens = relationship('Token', cascade='delete')


class Token(Base):
    __tablename__ = 'token'

    id = Column(INTEGER(unsigned=True), Sequence('token_id_seq'), primary_key=True, autoincrement=True)
    user_id = Column(INTEGER(unsigned=True), ForeignKey('user.id'), nullable=False)
    value = Column(String(511), unique=True, nullable=False)

I've been trying different things, including a @factory.post_generation method that adds groups & tokens to the user instance, but when I put a user in a fixture and use it in my test functions, these fields never show up. Do you have any recommendations on how to model this schema with Factory Boy?

Upvotes: 1

Views: 947

Answers (1)

metmirr
metmirr

Reputation: 4312

I am writing this for sqlalchemy part I dont know about the Factory boy. You already have a table which let you n to n relationship between users and groups(UserGroup table) and user-tokens for example:

id user_id group_id        id user_id value
1    5      3              1  5       adb45
2    5      4              2  5       xyz01
3    5      5
4    1      9
5    1      3    

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255), unique=True, nullable=False)

    tokens = relationship("Token", backref="user")

class Group(Base):
    __tablename__ = "groups"
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255), unique=True, nullable=False)

class UserGroup(Base):
    __tablename__ = "usergroups"
    id = Column(Integer, primary_key=True, autoincrement=True)
    group_id = Column(Integer, ForeignKey("Group.id"))
    user_id = Column(Integer, ForeignKey("User.id"))

class Token(Base):
    __tablename__ = "tokens"
    id = Column(Integer, primary_key=True, autoincrement=True)
    value = Column(String(511), unique=True, nullable=False)

    user_id = Column(Integer, ForeignKey("User.id"))

and the sqlalchemy documentation is good.

Upvotes: 0

Related Questions