Skip to content

Bug: using columns with system=True causes OperationalError exception #802

@AlexPetul

Description

@AlexPetul

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

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

Screenshots

No response

Logs

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)
<string>: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()
<string>: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 = <sqlalchemy.dialects.sqlite.pysqlite.SQLiteDialect_pysqlite object at 0x114416e90>, cursor = <sqlite3.Cursor object at 0x116108fc0>, statement = 'INSERT INTO authors (id, version) VALUES (?, ?)'
parameters = (6193, 'dCDXspBfUfcpFavQPjOE'), context = <sqlalchemy.dialects.sqlite.base.SQLiteExecutionContext object at 0x1140c3590>

    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[<lambda>0] - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table authors has no column named version

Release Version

3.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions