Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sqla_factory): added __set_association_proxy__ attribute #629

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

nisemenov
Copy link
Contributor

Description

  • __set_association_proxy__ attribute has been added in SQLAlchemyFactory class to add AssociationProxy type fields in the result FieldMeta factory dict;
  • appropriate tests have been added;
  • Usage Guide Configuration section has been updated.

Closes

@nisemenov nisemenov requested a review from guacs as a code owner January 15, 2025 12:28

from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory

if __version__.startswith("2"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required? It would be great to test this syntax for both 1 and 2


By default, relationships will not be set. This can be overridden via ``__set_relationships__``.
By default, ``__set_relationships__`` is set to ``False``. If it is ``True``, all fields with the SQLAlchemy `relationship() <relationship()_>`_ will be included in the resulting mock dictionary created by ``build`` method.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, ``__set_relationships__`` is set to ``False``. If it is ``True``, all fields with the SQLAlchemy `relationship() <relationship()_>`_ will be included in the resulting mock dictionary created by ``build`` method.
By default, ``__set_relationships__`` is set to ``False``. If it is ``True``, all fields with the SQLAlchemy `relationship() <relationship()_>`_ will be included in the result created by ``build`` method.


.. literalinclude:: /examples/library_factories/sqlalchemy_factory/test_example_2.py
:caption: Setting relationships
:language: python

.. note::
In general, foreign keys are not automatically generated by ``.build``. This can be resolved by setting the fields yourself and/or using ``create_sync``/ ``create_async`` so models can be added to a SQLA session so these are set.
In general, ForeignKey fields are automatically generated by ``build`` method because :class:`__set_foreign_keys__ <polyfactory.factories.sqlalchemy_factory.SQLAlchemyFactory.__set_foreign_keys__>` is set to ``True`` by default. But their values can be overwritten by using ``create_sync``/ ``create_async`` methods, so SQLAlchemy ORM creates them.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the last sentence correct always? Would that only be the case if a relationship is present?

Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/629

@nisemenov
Copy link
Contributor Author

Hi, @adhtruong!
Thanks for reviewing. I have fixed all issues.

More detailed about #629 (comment): I have rewritten this part more clearly.

@adhtruong
Copy link
Collaborator

Thanks for updating! LGTM

@adhtruong
Copy link
Collaborator

@all-contributors add @nisemenov for code doc

Copy link
Contributor

@adhtruong

I've put up a pull request to add @nisemenov! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants