[Python] "Type Error: argument of type 'method' is not iterabe" while initializing SQLAlchemy...

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a SQLAlchemy ORM with SqlAlchemy 2.0 using mapped_column() and Deklarative Base. My App starts with falcon and gunicorn in my PyCharm venv with Python3.12.

    The database tables are created correctly with:

    models.Base.metadata.create_all(self.engine)


    I have a Postgres database and all tables with their columns are created correctly, also manually creating an entry is working fine.

    But creating an entry using SQLAlchemy Object init fails, with an error I don't understand.

    from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship

    class Base(DeclarativeBase):
    pass

    class VirtualMachine(Base):
    __tablename__ = "virtual_machine"

    vm_id: Mapped[int] = mapped_column(primary_key=True)
    object_name: Mapped[str] = mapped_column()

    def __init__(self, object_name: str):
    self.vm_id = 1
    self.object_name = object_name

    def __repr__(self):
    pass

    def __dict__(self):
    pass


    It's not my first time using SQLAlchemy ORM for database management, but I don't have a clue what this error message means while trying to create an object 'VirtualMachine' in my unit tests.

    class TestVM(unittest.TestCase):
    def setUp(self):
    self.data = {
    "object_name": "test-vm"
    }
    engine = sqlalchemy.create_engine(connect_str)
    self.db = engine.connect()

    def test_create_vm(self):
    self.test_vm = VirtualMachine(**self.data)


    Error
    Traceback (most recent call last):
    File "/api/tests/orm_test/vm_object_test.py", line 51, in test_create_vm
    self.test_vm = VirtualMachine(**self.data)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "", line 4, in init
    File "/api/venv/lib64/python3.12/site-packages/sqlalchemy/orm/state.py", line 571, in initialize_instance
    with util.safe_reraise():
    File "/api/venv/lib64/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in exit
    raise exc_value.with_traceback(exc_tb)
    File "/api/venv/lib64/python3.12/site-packages/sqlalchemy/orm/state.py", line 569, in initialize_instance
    manager.original_init(*mixed[1:], **kwargs)
    File "/api/src/database/models.py", line 58, in init
    self.vm_id = 1
    ^^^^^^^^^^
    File "/api/venv/lib64/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 537, in set
    self.impl.set(
    File "/api/venv/lib64/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1272, in set
    old = self.get(state, dict
    , PASSIVE_RETURN_NO_VALUE)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "api/venv/lib64/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1074, in get
    if self.key in dict
    :
    ^^^^^^^^^^^^^^^^^

    TypeError: argument of type 'method' is not iterable
    ​

    Does anyone have any idea why it can't be initialized correctly?

    I also tried with other data types, because in the beginning, I had uuid.UUID as Mapped[type], and I had asyncio integrated which I now keep out everywhere and started a wsgi app now.

    I thought it might be a problem with the session, but I think it isn't.

    Continue reading...

Compartilhe esta Página