99from .gino import Gino , GinoEngine
1010from .settings .globals import GLOBALS
1111
12+ # Explicitly register the gino asyncpg dialect with SQLAlchemy
13+ # This ensures it's available before any database connections are attempted
14+ try :
15+ from sqlalchemy .dialects import registry
16+ import gino .dialects .asyncpg
17+ registry .register ("asyncpg" , "gino.dialects.asyncpg" , "AsyncpgDialect" )
18+ registry .register ("postgresql.asyncpg" , "gino.dialects.asyncpg" , "AsyncpgDialect" )
19+ except Exception :
20+ # If registration fails, the normal import should still work
21+ pass
22+
1223READ_ENGINE : Optional [GinoEngine ] = None
1324SessionLocal : Optional [Session ] = None
1425Base = declarative_base ()
2031
2132db = Gino (
2233 app ,
34+ driver = "asyncpg" ,
2335 host = GLOBALS .database_config .host ,
2436 port = GLOBALS .database_config .port ,
2537 user = GLOBALS .database_config .username ,
@@ -41,8 +53,20 @@ def get_synchronous_db() -> Iterator[Session]:
4153 raise RuntimeError ("No database url set." )
4254
4355 if SessionLocal is None :
44- db_conn = GLOBALS .database_config .url
45- engine = create_engine (db_conn , pool_size = 5 , max_overflow = 0 )
56+ # Create a synchronous database URL using psycopg2 instead of asyncpg
57+ # asyncpg only works with async engines, not synchronous ones
58+ from sqlalchemy .engine .url import URL
59+
60+ db_config = GLOBALS .database_config
61+ sync_url = URL (
62+ drivername = "postgresql+psycopg2" ,
63+ username = db_config .username ,
64+ password = str (db_config .password ) if db_config .password else None ,
65+ host = db_config .host ,
66+ port = db_config .port ,
67+ database = db_config .database ,
68+ )
69+ engine = create_engine (sync_url , pool_size = 5 , max_overflow = 0 )
4670 SessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = engine )
4771
4872 synchronous_db : Optional [Session ] = None
0 commit comments