- add example add, update, delete

This commit is contained in:
marys
2025-05-27 12:47:53 +02:00
parent 9f995f090b
commit a3e810747f

View File

@@ -33,3 +33,33 @@ class MtrAdditionalNodeQuery:
async with self.db() as session:
stmt = select(MtrAdditionalNodesModel).where(MtrAdditionalNodesModel.id == node_id)
return (await session.execute(stmt)).scalars().first()
# ADD example from Perplexity
# async def insert_city(session: AsyncSession, name: str, population: int):
# new_city = City(name=name, population=population)
# session.add(new_city)
# await session.commit() # Commits the transaction
# await session.refresh(new_city) # Refreshes to get generated fields (like id)
# return new_city
# DELETE
# async def delete_city_by_id(session: AsyncSession, city_id: int):
# # First, fetch the city you want to delete
# result = await session.execute(select(City).where(City.id == city_id))
# city = result.scalar_one_or_none()
# if city:
# await session.delete(city)
# await session.commit()
# return True # Indicate success
# return False # Not found
# UPDATE
# async def update_city_population(session: AsyncSession, city_id: int, new_population: int):
# result = await session.execute(select(City).where(City.id == city_id))
# city = result.scalar_one_or_none()
# if city:
# city.population = new_population
# await session.commit()
# await session.refresh(city) # Optional: refresh to get updated values
# return city
# return None