Suppose I have table tags
which has a field count
that indicates how many items
have been tagged with the given tag.
How do I
I got here following a tutorial for FastAPI, SQLAlchemy and databases (async). After various tries I finally got it working with following code in my crud.py / control layer.
async def inc_counter():
query = counters.select(counters.c.id==1)
current_val = await database.fetch_val(query=query, column=1)
query = counters.update().where(counters.c.id==1).values(counter=current_val+1).returning(counters.c.counter)
return await database.execute(query=query)
My table looks like this:
counters = Table('counters', metadata,
Column('id', Integer, primary_key=True),
Column('counter', Integer)
)