polyfactory
polyfactory copied to clipboard
Bug: String length ignored when generating values for SQLAlchemy models
Description
Polyfactory's SQLAlchemyFactory doesn't seem to respect the string length specified when populating a SQLAlchemy model. For example, a field defined as mapped_column(String(1)) is consistently assigned a 20 character string. Ditto for a field defined as mapped_column(String(100))–also consistently a 20 character string. (20 character strings seem to be the default length for the underlying faker.pystr() provider.)
URL to code causing the issue
No response
MCVE
from sqlalchemy import create_engine, Engine, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory
class Base(DeclarativeBase): ...
class Cat(Base):
__tablename__ = "cats"
id: Mapped[str] = mapped_column(String(10), primary_key=True)
name: Mapped[str] = mapped_column(String(10))
class CatFactory(SQLAlchemyFactory[Cat]): ...
cat = CatFactory.build()
len(cat.id) # 20
len(cat.name) # 20
Steps to reproduce
The MCVE code should be adequate to reproduce the issue with the latest (2.17.0) version of polyfactory.
Release Version
2.17.0
Platform
- [ ] Linux
- [X] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)
Big thanks to @adhtruong for fixing this and to @guacs for the review!