-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
42 lines (32 loc) · 1.48 KB
/
database.py
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
from sqlalchemy import create_engine, Column, String, Integer, ForeignKey, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, Session
from datetime import datetime
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class Player(Base):
__tablename__ = "players"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
updated_at = Column(DateTime, default=datetime.utcnow)
missions_passed = Column(Integer, default=0)
room_id = Column(Integer, ForeignKey('rooms.id'))
room = relationship("Room", back_populates="players")
class Room(Base):
__tablename__ = "rooms"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
session_id = Column(Integer, ForeignKey('sessions.id'))
session = relationship("Session", back_populates="rooms")
players = relationship("Player", back_populates="room")
class Session(Base):
__tablename__ = "sessions"
id = Column(String, primary_key=True, index=True)
created_at = Column(DateTime, default=datetime.utcnow)
rooms = relationship("Room", back_populates="session")
Base.metadata.create_all(bind=engine)