-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_config.py
More file actions
25 lines (19 loc) · 754 Bytes
/
Copy pathdb_config.py
File metadata and controls
25 lines (19 loc) · 754 Bytes
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
# db_config.py
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker, declarative_base
# URL database SQLite
DATABASE_URL = "sqlite:///./restorify.db"
# Membuat engine SQLAlchemy
engine = create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}, echo=True # echo=True untuk debugging
)
# Membuat kelas SessionLocal
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Membuat Base kelas deklaratif
Base = declarative_base()
# Mengaktifkan foreign key constraints di SQLite
@event.listens_for(engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()