'Exception related to async Sqlalchemy

I wrote my sqlalchemy models, so i needed to retrieve related objects when getting parent.

User model

class User(AbstractBaseModel):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String, unique=True, index=True, nullable=False)
    hashed_password = Column(String, nullable=False)
    is_active = Column(Boolean, default=True)
    tests = relationship("Test", backref=backref("holder", lazy="subquery"))
    sessions = relationship("Session")

Test model

class Test(AbstractBaseModel):
    __tablename__ = "tests"

    title = Column(String(256))
    holder_id = Column(Integer, ForeignKey("users.id"))
    published = Column(Boolean)
    questions = relationship("Question")
    sessions = relationship("Session")

So when i am trying to retreive holder object i am getting an sqlalchemy.orm.exc.DetachedInstanceError:

Parent instance <Test at 0x7f5e391522c0> is not bound to a Session; lazy load operation of attribute 'holder' cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3)

it raises after i am using this code:

async def create(self, obj):
    await self._before_create()

    self._database_session.add(obj)
    await self._database_session.commit()
    await self._after_create()

I tried all types of lazyloading, and tried to add separate holder attr with relationship in tests but it did not help. If someone knows how to get related objects in async sqlalchemy please let me know as soon as possible.

So it is debugger screenshot: enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source