0

My question is the exact copy of this [question]1, but I'm asking it for syntax version > 1.4

I'm using the next syntax in my queries:

get_user_st = users.select().where(users.c.login == user.phone_number)
connection.execute(statement=get_user_st).fetchone()

Getting db:

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import SQLAlchemyError


engine_async = create_async_engine(url=config.SQLALCHEMY_ASYNCPG_URL, echo=False, future=True)  # Future -use style 2.0
session_async = sessionmaker(bind=engine_async, autoflush=True, class_=AsyncSession)

async def get_db():
    session = session_async()
    try:
        yield session
        await session.commit()
    except SQLAlchemyError as ex:
        await session.rollback()
        raise ex
    finally:
        await session.close()

Type of my session is:

type(postgres_session)
<class 'sqlalchemy.orm.session.AsyncSession'>

P.S. It's preferable to get not the current primary key but an upcoming/next

salius
  • 918
  • 1
  • 14
  • 30
  • Why do you need to know the PK? _And why would you even like to know the "next" one, which likely completely defeats the purpose of allowing concurrency?_ – van Jun 08 '21 at 06:51
  • @van Thanks for the hint! It's just for a clean code. I have the next code: `if condition_1: some_filed = some value; else pass; inserted_id = crud.insert_item(item); if condition_1: crud.update_some_item(id=inserted_id)`. I jist wanted to combine this two conditions. Maybe Sqlalchemy adopting to book primary key by the query, but for now it's more easely to just use a flag. – salius Jun 08 '21 at 13:45

0 Answers0