File size: 1,100 Bytes
babeaf6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from dotenv import load_dotenv
import os
load_dotenv()
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL")
connect_args = {"check_same_thread": False} if SQLALCHEMY_DATABASE_URL.startswith(
"sqlite") else {}
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args=connect_args
)
SessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
if __name__ == "__main__":
print(SQLALCHEMY_DATABASE_URL)
from realtime_ai_character.models.user import User
from realtime_ai_character.models.interaction import Interaction
with SessionLocal() as session:
print(session.query(User).all())
session.delete(User(name="Test", email="[email protected]"))
session.commit()
print(session.query(User).all())
session.query(User).filter(User.name == "Test").delete()
session.commit()
print(session.query(User).all())
|