Aryan Mishra commited on
Commit
2417b88
·
1 Parent(s): 4347000

Allow SQLite DB connections across threads

Browse files

Add SQLite-specific `connect_args` when creating the SQLAlchemy engine so `check_same_thread` is disabled for local SQLite usage while preserving existing settings for other databases.

Files changed (1) hide show
  1. api/middleware/dependencies.py +6 -1
api/middleware/dependencies.py CHANGED
@@ -13,10 +13,15 @@ if not DATABASE_URL:
13
  "Please set it in your .env file or environment."
14
  )
15
 
16
- engine = create_engine(DATABASE_URL, pool_pre_ping=True)
 
 
 
 
17
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
18
 
19
 
 
20
  def get_db():
21
  db = SessionLocal()
22
  try:
 
13
  "Please set it in your .env file or environment."
14
  )
15
 
16
+ connect_args = {}
17
+ if DATABASE_URL.startswith("sqlite"):
18
+ connect_args["check_same_thread"] = False
19
+
20
+ engine = create_engine(DATABASE_URL, pool_pre_ping=True, connect_args=connect_args)
21
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
22
 
23
 
24
+
25
  def get_db():
26
  db = SessionLocal()
27
  try: