|
@@ -1,4 +1,5 @@
|
|
|
-from sqlalchemy import create_engine
|
|
|
+from sqlalchemy import create_engine , text
|
|
|
+from app.models.base import Base
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
import os
|
|
@@ -13,7 +14,7 @@ logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# 创建Base类
|
|
|
-Base = declarative_base()
|
|
|
+#Base = declarative_base()
|
|
|
Base.metadata.clear()
|
|
|
|
|
|
# 加载环境变量
|
|
@@ -98,11 +99,37 @@ def execute_sql(sql_statement):
|
|
|
# 新增:自动创建数据库表(关键!)
|
|
|
def create_tables():
|
|
|
try:
|
|
|
+ # ✨ 新增:同时启用 PostGIS 核心和 Raster 扩展
|
|
|
+ with engine.begin() as conn:
|
|
|
+ # 启用 PostGIS 核心(矢量)
|
|
|
+ conn.execute(text("CREATE EXTENSION IF NOT EXISTS postgis;")) # pyright: ignore[reportUndefinedVariable]
|
|
|
+ # 启用 PostGIS Raster(栅格)
|
|
|
+ conn.execute(text("CREATE EXTENSION IF NOT EXISTS postgis_raster;")) # pyright: ignore[reportUndefinedVariable]
|
|
|
+ logger.info("PostGIS 核心及 Raster 扩展已启用(或已存在)")
|
|
|
# 必须导入所有模型,否则 Base 不知道要创建哪些表
|
|
|
# 替换成你项目中实际的模型文件路径(根据你的目录结构调整)
|
|
|
from app.models.orm_models import Base # 确保模型继承自这个 Base
|
|
|
from app.models.vector import VectorData # 导入需要创建的表
|
|
|
-
|
|
|
+ #from app.models.raster import RasterData # 补充其他模型
|
|
|
+ from app.models.county import County
|
|
|
+ from app.models.farmland import FarmlandData
|
|
|
+
|
|
|
+ from app.models.agricultural import AgriculturalData # 假设类名是 Agricultural
|
|
|
+ from app.models.assessment import Assessment
|
|
|
+ from app.models.atmo_company import AtmoCompany
|
|
|
+ from app.models.atmo_sample import AtmoSampleData
|
|
|
+ from app.models.CropCd_input import CropCdInputData
|
|
|
+ from app.models.CropCd_output import CropCdOutputData
|
|
|
+ from app.models.cross_section import CrossSection
|
|
|
+ from app.models.EffCd_input import EffCdInputData
|
|
|
+ from app.models.EffCd_output import EffCdOutputData
|
|
|
+ from app.models.FluxCd_input import FluxCdInputData
|
|
|
+ from app.models.FluxCd_output import FluxCdOutputData
|
|
|
+ from app.models.MSM_input import MSMInputData
|
|
|
+ from app.models.MSM_output import MSMOutputData
|
|
|
+ from app.models.parameters import Parameters
|
|
|
+ from app.models.soil import SoilData
|
|
|
+ from app.models.water_sample import WaterSampleData
|
|
|
# 创建所有表
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
logger.info("数据库表自动创建成功!")
|