2020-07-21 09:02:44 +02:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy import Column, Integer, String
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
|
|
|
|
2020-07-21 09:37:15 +02:00
|
|
|
engine = create_engine("sqlite:///:memory:", echo=True)
|
2020-07-21 09:02:44 +02:00
|
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
|
|
class Toaster(Base):
|
2020-07-21 09:37:15 +02:00
|
|
|
__tablename__ = "toasters"
|
2020-07-21 09:02:44 +02:00
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
color = Column(String)
|
|
|
|
|
|
|
|
|
|
|
|
def toaster_exists_bad(toaster_id):
|
|
|
|
session = Session()
|
|
|
|
return bool(session.query(Toaster).filter_by(id=toaster_id).first())
|
|
|
|
|
|
|
|
|
|
|
|
def toaster_exists_good(toaster_id):
|
|
|
|
session = Session()
|
|
|
|
query = session.query(Toaster).filter_by(id=toaster_id)
|
|
|
|
return session.query(query.exists()).scalar()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
|
|
|
|
toaster_exists_bad(1)
|
|
|
|
toaster_exists_good(2)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|