fix(module_generator): 修复代码生成器关键问题及模板路径错误

- 修正 CRUD 基类初始化时 model 参数传递方式,改为实例化对象
- 修改部分查询中条件字段名的错误,table_id 改为 id
- 修正数据库方言下查询语句缺少 SELECT 关键字的问题
- 优化查询参数绑定,避免直接传递包含分页参数的字典
- 修正生成表结构后返回数据中的主键字段 id 替代 table_id
- 优化删除逻辑中获取列 ID 的字段名称错误
- 重写建表功能相关代码,添加异常捕获和事务回滚
- 取消子表字段 Schema 的非空强制,改为可选类型
- 修正前端接口请求路径及参数命名,使其更加直观和规范
- 修改模板加载路径为绝对路径,确保多环境下模板加载正确
- 修复模板工具中子表相关属性为空时的类型检查错误
- 调整模板文件及路径后缀名,统一从 .jinja2 改为 .j2
- 修正生成文件路径映射以符合新模板路径和文件名规范
- 删除冗余和废弃的 Python 控制器及 CRUD 模板文件
- 修正 SQL 菜单模板中模块名路径格式错误
- 更新前端 Vue API 调用函数参数与接口路径一致性
This commit is contained in:
zhangtao
2025-09-22 01:10:24 +08:00
parent 9feb08a962
commit f78a419fb8
15 changed files with 566 additions and 233 deletions
@@ -9,6 +9,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from typing import Any, List, Dict, Optional, Sequence
from app.config.setting import settings
from app.core.base_model import CamelCaseUtil
from app.core.exceptions import CustomException
from app.utils.gen_util import GenUtils
from app.utils.template_util import TemplateInitializer, TemplateUtils
@@ -116,7 +117,7 @@ class GenTableService:
GenUtils.init_table(table, current_user.username) # 使用username而不是user.user_name
add_gen_table = await gen_table_dao.create(data=table.model_dump())
if add_gen_table:
table.table_id = add_gen_table.table_id
table.table_id = add_gen_table.id
gen_table_columns = await gen_table_column_dao.get_gen_db_table_columns_by_name(auth.db, table_name or "")
for column in [
GenTableColumnSchema(**gen_table_column)
@@ -217,7 +218,7 @@ class GenTableService:
# 这里需要先查询出所有相关的column_id,然后删除
columns = await gen_table_column_dao.get_gen_table_column_list_by_table_id(auth.db, int(table_id))
if columns:
column_ids = [column.column_id for column in columns]
column_ids = [column.id for column in columns]
await gen_table_column_dao.delete(ids=column_ids)
if isinstance(auth.db, AsyncSession):
await auth.db.commit()
@@ -281,8 +282,25 @@ class GenTableService:
:param current_user: 当前用户信息对象
:return: 创建表结构结果
"""
# 移除sqlglot相关代码,因为导入失败
raise CustomException(msg='建表功能暂不可用')
# 确保db是AsyncSession类型
if not isinstance(auth.db, AsyncSession):
raise CustomException(msg='数据库会话类型不正确')
gen_table_dao = GenTableDao(auth=auth)
try:
# 执行SQL语句创建表
await gen_table_dao.create_table_by_sql_dao(auth.db, [sql])
if isinstance(auth.db, AsyncSession):
await auth.db.commit()
return SuccessResponse(msg='创建表结构成功')
except Exception as e:
if isinstance(auth.db, AsyncSession):
try:
await auth.db.rollback()
except:
pass # 忽略回滚错误
raise CustomException(msg=f'创建表结构失败: {str(e)}')
@classmethod
async def preview_code_services(cls, auth: AuthSchema, table_id: int) -> dict[Any, Any]: