ext_database.py 798 B

12345678910111213141516171819202122232425
  1. from sqlalchemy import create_engine, MetaData
  2. from sqlalchemy.orm import sessionmaker, scoped_session
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from common import config
  5. # 定义元数据和命名约定
  6. POSTGRES_INDEXES_NAMING_CONVENTION = {
  7. "ix": "ix_%(column_0_label)s",
  8. "uq": "uq_%(table_name)s_%(column_0_name)s",
  9. "ck": "ck_%(table_name)s_%(constraint_name)s",
  10. "fk": "fk_%(table_name)s_%(column_0_name)s",
  11. "pk": "pk_%(table_name)s"
  12. }
  13. metadata = MetaData(naming_convention=POSTGRES_INDEXES_NAMING_CONVENTION)
  14. # SQLAlchemy 基础设置
  15. Base = declarative_base(metadata=metadata)
  16. engine = create_engine(config.DB_URL)
  17. Session = scoped_session(sessionmaker(bind=engine))
  18. # 初始化数据库
  19. def init_db():
  20. pass
  21. # Base.metadata.create_all(bind=engine)