PsychoScalesOrg/database.py
mxr612 b8f65547b2 fix: change database commit to flush in new_user function
- Updated the new_user function in database.py to use db.flush() instead of db.commit() for adding new users. This change ensures that the user object is added to the session without committing the transaction, allowing for better control over database operations.
2025-06-19 00:47:32 +08:00

62 lines
1.7 KiB
Python

from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, JSON, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
import json
from datetime import datetime, UTC
SQLALCHEMY_DATABASE_URL = "sqlite:///./psychoscales.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
json_serializer=lambda obj: json.dumps(obj, ensure_ascii=False)
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
created_at = Column(DateTime)
last_seen = Column(DateTime)
responses = relationship("ScaleResult", back_populates="user")
class ScaleResult(Base):
__tablename__ = "responses"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
scale_id = Column(String, index=True)
user_agent = Column(String)
ip_address = Column(String)
location = Column(JSON)
raw_response = Column(JSON)
sum_response = Column(JSON)
avg_response = Column(JSON)
created_at = Column(DateTime)
user = relationship("User", back_populates="responses")
# Create tables
Base.metadata.create_all(bind=engine)
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def new_user() -> int:
db = SessionLocal()
try:
with db.begin():
user = User()
user.last_seen = user.created_at = datetime.now(UTC)
db.add(user)
db.flush()
return user.id
finally:
db.close()