I am stuck into a problem with aiopg. There are a one problem when i async with engine and try to return a result to endpoint in FastApi. Look down below for my db class.
class DBEngine:
def __init__(self, connection_url: str) -> None:
self.dsn = connection_url
self.engine: Optional[Engine] = None
async def connect(self) -> None:
self.engine = await create_engine(dsn=self.dsn, maxsize=100)
@property
def client(self) -> Engine:
if self.engine:
return self.engine
raise Exception("Not connected to database")
async def close(self) -> None:
if self.engine:
self.engine.close()
await self.engine.wait_closed()
As you can see there is a property, i use it in a code of method of a model class:
class Products(Base):
__tablename__ = "products"
UUID = sa.Column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
unique=True,
nullable=False,
)
product_name = sa.Column(sa.String())
@classmethod
async def create_loan(
cls,
*,
product_name: str
) -> "Products":
query = cls.insert_query(
product_name=product_name
)
async with db_engine.client.acquire() as conn:
cursor = await conn.execute(query)
return await cursor.fetchone()
And finally i'll show an endpoint in fastapi:
@routes.post("/api/v1/loans")
async def create_loan(loan: Loan):
product = await Products.create_loan(product_name=loan.product_name)
return {"loan_id": product.UUID}
ERROR LOG:
**Traceback**
...
aiopg.sa.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.
Please tell me what's wrong, i spent so much time to solve this problem. Feel free to answer, thanks.