I get an error running this code. The error: The "entities" argument to Select.with_only_columns(), when referring to a sequence of items, is now passed as a series of positional elements, rather than as a list.
Im noob and now studying python and sqlalchemy, could you assist me and make this code work?
TY in advance!
My code:
SessionLocal()
is
SessionLocal = sessionmaker(
bind=engine,
class_=AsyncSession, # Используем асинхронный сеанс
expire_on_commit=False,
autocommit=False,
autoflush=False
)
async def get_claim_info(claim_id):
async with SessionLocal() as session:
try:
stmt = select(Support.id, Support.Status, Support.Text).filter(Support.id == claim_id)
result = await session.execute(stmt)
claim_info = result.fetchone()
if claim_info:
claim_text = f"1: {claim_info[0]}\n2: {claim_info[1]}\n3: {claim_info[2]}"
return claim_text
else:
return "none"
except Exception as e:
print("Error:", e)
return "Error"
finally:
# Закрываем сессию после выполнения операции
await session.close()
I have tried different ways, for example
stmt = select(Support.id, Support.Status, Support.Text).filter(Support.id == claim_id).with_only_columns(Support.id, Support.Status, Support.Text)
or
stmt = select(Support).filter(Support.id == claim_id).with_only_columns(Support.id, Support.Status, Support.Text)
and
claim_text = f"1: {claim_info.id}\n2: {claim_info.Status}\n3: {claim_info.Text}"
This is relatively easy to fix. The API for this SQLAlchemy function has just changed recently.
You didn't provide complete information in your question, so I will have to infer some information from the limited context available, however it should be enough to change this line:
with_only_columns(Support.columns.id, Support.columns.Status, Support.columns.Text)
You can read more about it here.