Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import date
- from sqlalchemy import Column, Date, Integer, Row, create_engine, select
- from sqlalchemy.orm import DeclarativeBase, Mapped, Session
- engine = create_engine("sqlite://")
- class Base(DeclarativeBase):
- pass
- class Payment(Base):
- __tablename__ = "payment"
- id: Mapped[int] = Column(Integer, primary_key=True, autoincrement=False)
- date_paid: Mapped[date] = Column(Date)
- def __repr__(self) -> str:
- return f"Payment(id={repr(self.id)}, date_paid={repr(self.date_paid)})"
- Base.metadata.create_all(engine)
- # example data
- with Session(engine) as sess:
- sess.add(Payment(id=1, date_paid=date(2001, 1, 1)))
- sess.commit()
- with Session(engine) as sess:
- row: Row = sess.execute(select(Payment).where(Payment.id == 1)).one()
- print(type(row)) # <class 'sqlalchemy.engine.row.Row'>
- pmt_1: Payment = row[0]
- print(pmt_1) # Payment(id=1, date_paid=datetime.date(2001, 1, 1))
- print(f">>> object is{'' if pmt_1 in sess else ' NOT'} in the session")
- # >>> object is in the session
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement