I want to save new objects in DB and receive autoincrement ID of new entity without call session.flush(). I tried autoflush=True (docs) option but had no luck.
Here my code.
from sqlalchemy import create_engine, Column, Integer, String, Boolean
from sqlalchemy.orm import sessionmaker, as_declarative
engine = create_engine("postgresql://postgres:@localhost/postgres")
Session = sessionmaker(autocommit=False, autoflush=True, bind=engine)
session = Session()
print(session.autoflush) # -> True
@as_declarative()
class Base:
__name__: str
class UserDb(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
username: str = Column(String, unique=True)
hashed_password: str = Column(String)
is_active: bool = Column(Boolean, default=True)
is_superuser: bool = Column(Boolean, default=False)
user = UserDb(
username="username",
hashed_password="password",
)
session.add(user)
# Here I want to have user.id
print(user.id) # -> None
session.flush()
print(user.id) # -> 12
Is there a way to achieve this behavior in SQLAlchemy?
Env: python 3.9.7, SQLAlchemy 1.4.27