litestar-org / litestar-org/polyfactory
Bug: using columns with `system=True` causes `OperationalError` exception
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 120
- PR merge metrics
- No merged PRs in 30d
Description
### Description
System columns (PostgreSQL) are not skipped during object creation.
The behaviour is similar to `Computed` columns in a sense that we only care about it during persistence. When using the `build` method, values can still be generated normally. But when persistence is involved, these fields should be skipped i guess
### URL to code causing the issue
_No response_
### MCVE
```python
class Author(Base):
__tablename__ = "authors"
id: Any = Column(Integer(), primary_key=True)
version: Any = Column(String, system=True)
books: Any = orm.relationship(
"Book",
collection_class=list,
uselist=True,
back_populates="author",
)
```
### Steps to reproduce
```bash
```
### Screenshots
_No response_
### Logs
```bash
polyfactory/factories/base.py:1202: in create_sync
return cls._get_sync_persistence().save(data=cls.build(**kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
polyfactory/factories/sqlalchemy_factory.py:53: in save
self.session.commit()
.venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py:2030: in commit
trans.commit(_to_root=True)
:2: in commit
???
.venv/lib/python3.13/site-packages/sqlalchemy/orm/state_changes.py:137: in _go
ret_value = fn(self, *arg, **kw)
^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py:1311: in commit
self._prepare_impl()
:2: in _prepare_impl
???
.venv/lib/python3.13/site-packages/sqlalchemy/orm/state_changes.py:137: in _go
ret_value = fn(self, *arg, **kw)
^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py:1286: in _prepare_impl
self.session.flush()
.venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py:4331: in flush
self._flush(objects)
.venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py:4466: in _flush
with util.safe_reraise():
^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.13/site-packages/sqlalchemy/util/langhelpers.py:224: in __exit__
raise exc_value.with_traceback(exc_tb)
.venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py:4427: in _flush
flush_context.execute()
.venv/lib/python3.13/site-packages/sqlalchemy/orm/unitofwork.py:466: in execute
rec.execute(self)
.venv/lib/python3.13/site-packages/sqlalchemy/orm/unitofwork.py:642: in execute
util.preloaded.orm_persistence.save_obj(
.venv/lib/python3.13/site-packages/sqlalchemy/orm/persistence.py:93: in save_obj
_emit_insert_statements(
.venv/lib/python3.13/site-packages/sqlalchemy/orm/persistence.py:1048: in _emit_insert_statements
result = connection.execute(
.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:1419: in execute
return meth(
.venv/lib/python3.13/site-packages/sqlalchemy/sql/elements.py:526: in _execute_on_connection
return connection._execute_clauseelement(
.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:1641: in _execute_clauseelement
ret = self._execute_context(
.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:1846: in _execute_context
return self._exec_single_context(
.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:1986: in _exec_single_context
self._handle_dbapi_exception(
.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:2355: in _handle_dbapi_exception
raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
.venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py:1967: in _exec_single_context
self.dialect.do_execute(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = , cursor = , statement = 'INSERT INTO authors (id, version) VALUES (?, ?)'
parameters = (6193, 'dCDXspBfUfcpFavQPjOE'), context =
def do_execute(self, cursor, statement, parameters, context=None):
> cursor.execute(statement, parameters)
E sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table authors has no column named version
E [SQL: INSERT INTO authors (id, version) VALUES (?, ?)]
E [parameters: (6193, 'dCDXspBfUfcpFavQPjOE')]
E (Background on this error at: https://sqlalche.me/e/20/e3q8)
.venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py:951: OperationalError
=============================================================================================== warnings summary ================================================================================================
tests/test_generics.py:8
/Users/alexpetul/Desktop/polyfactory/tests/test_generics.py:8: PydanticDeprecatedSince20: `pydantic.generics:GenericModel` has been moved to `pydantic.BaseModel`. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
from pydantic.generics import GenericModel
tests/test_optional_model_field_inference.py:10
/Users/alexpetul/Desktop/polyfactory/tests/test_optional_model_field_inference.py:10: PydanticDeprecatedSince20: `pydantic.generics:GenericModel` has been moved to `pydantic.BaseModel`. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
from pydantic.generics import GenericModel
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================================================================================ short test summary info ============================================================================================
FAILED tests/sqlalchemy_factory/test_sqlalchemy_factory_common.py::test_sync_persistence[0] - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table authors has no column named version
```
### Release Version
3.0
### Platform
- [x] Linux
- [x] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)
Contributor guide
Research direction
Start in polyfactory/factories/sqlalchemy_factory.py, following the persistence path from create_sync in polyfactory/factories/base.py. Reproduce the failure with tests/sqlalchemy_factory/test_sqlalchemy_factory_common.py::test_sync_persistence and inspect how SQLAlchemy model fields are included in persistence. Done means system columns are excluded from persisted inserts without changing normal build behavior, and the persistence test passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlalchemy
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100