fix: 修复代码生成主子表功能异常的问题

This commit is contained in:
insistence
2025-02-18 17:46:37 +08:00
parent fd07ad088c
commit 0fdf45c73f
7 changed files with 56 additions and 14 deletions

View File

@@ -145,7 +145,7 @@ class {{ BusinessName }}Dao:
:param {{ businessName }}: {{ functionName }}对象
:return:
"""
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump(exclude={% raw %}{{% endraw %}{% if table.sub %}'{{ subclassName }}_list, '{% endif %}{% for column in columns %}{% if not column.insert %}'{{ column.python_field | camel_to_snake }}'{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% raw %}}{% endraw %}))
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump(exclude={% raw %}{{% endraw %}{% if table.sub %}'{{ subclassName }}_list', {% endif %}{% for column in columns %}{% if not column.insert %}'{{ column.python_field | camel_to_snake }}'{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% raw %}}{% endraw %}))
db.add(db_{{ businessName }})
await db.flush()

View File

@@ -32,7 +32,7 @@ class {{ subClassName }}(Base):
__tablename__ = '{{ subTableName }}'
{% for column in subTable.columns %}
{{ column.column_name }} = Column({{ column.column_type | get_sqlalchemy_type }}, {% if column.pk %}primary_key=True, {% endif %}{% if column.increment %}autoincrement=True, {% endif %}{% if column.required %}nullable=True{% else %}nullable=False{% endif %}, comment='{{ column.column_comment }}')
{{ column.column_name }} = Column({{ column.column_type | get_sqlalchemy_type }}, {% if column.column_name == subTableFkName %}ForeignKey('{{ tableName }}.{{ subTableFkName }}'), {% endif %}{% if column.pk %}primary_key=True, {% endif %}{% if column.increment %}autoincrement=True, {% endif %}{% if column.required %}nullable=True{% else %}nullable=False{% endif %}, comment='{{ column.column_comment }}')
{% endfor %}
{% if table.sub %}

View File

@@ -103,7 +103,7 @@ class {{ BusinessName }}Service:
:param page_object: 编辑{{ functionName }}对象
:return: 编辑{{ functionName }}校验结果
"""
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True, exclude={% raw %}{{% endraw %}{% if table.sub %}'{{ subclassName }}_list, '{% endif %}{% for column in columns %}{% if not column.edit and not column.pk %}'{{ column.python_field | camel_to_snake }}'{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% raw %}}{% endraw %})
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True, exclude={% raw %}{{% endraw %}{% if table.sub %}'{{ subclassName }}_list', {% endif %}{% for column in columns %}{% if not column.edit and not column.pk %}'{{ column.python_field | camel_to_snake }}'{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% raw %}}{% endraw %})
{{ businessName }}_info = await cls.{{ businessName }}_detail_services(query_db, page_object.{{ pk_field }})
if {{ businessName }}_info.{{ pk_field }}:
{% for column in columns %}
@@ -117,8 +117,10 @@ class {{ BusinessName }}Service:
try:
await {{ BusinessName }}Dao.edit_{{ businessName }}_dao(query_db, edit_{{ businessName }})
{% if table.sub %}
for sub_table in {{ businessName }}_info.{{ subclassName }}_list:
await {{ BusinessName }}Dao.delete_{{ subTable.business_name }}_dao(query_db, sub_table)
for sub_table in page_object.{{ subclassName }}_list:
await {{ BusinessName }}Dao.edit_{{ subTable.business_name }}_dao(query_db, sub_table)
await {{ BusinessName }}Dao.add_{{ subTable.business_name }}_dao(query_db, sub_table)
{% endif %}
await query_db.commit()
return CrudResponseModel(is_success=True, message='更新成功')
@@ -141,12 +143,12 @@ class {{ BusinessName }}Service:
{{ pk_field }}_list = page_object.{{ pk_field }}s.split(',')
try:
for {{ pk_field }} in {{ pk_field }}_list:
await {{ BusinessName }}Dao.delete_{{ businessName }}_dao(query_db, {{ BusinessName }}Model({{ pkField }}={{ pk_field }}))
{% if table.sub %}
{{ businessName }} = await cls.{{ businessName }}_detail_services(query_db, int({{ pk_field }}))
for sub_table in {{ businessName }}.{{ subclassName }}_list:
await {{ BusinessName }}Dao.delete_{{ subTable.business_name }}_dao(query_db, sub_table)
{% endif %}
await {{ BusinessName }}Dao.delete_{{ businessName }}_dao(query_db, {{ BusinessName }}Model({{ pkField }}={{ pk_field }}))
await query_db.commit()
return CrudResponseModel(is_success=True, message='删除成功')
except Exception as e:

View File

@@ -32,7 +32,8 @@ from typing import Optional
from module_admin.annotation.pydantic_annotation import as_query
class {{ BusinessName }}Model(BaseModel):
{% if table.sub %}
class {{ BusinessName }}BaseModel(BaseModel):
"""
{{ functionName }}表对应pydantic模型
"""
@@ -42,9 +43,6 @@ class {{ BusinessName }}Model(BaseModel):
{% for column in columns %}
{{ column.column_name }}: Optional[{{ column.python_type }}] = Field(default=None, description='{{ column.column_comment }}')
{% endfor %}
{% if table.sub %}
{{ subclassName }}_list: Optional[List['{{ subTable.business_name | capitalize }}Model']] = Field(default=None, description='子表列信息')
{% endif %}
{% for column in columns %}
{% if column.required %}
@@ -53,6 +51,7 @@ class {{ BusinessName }}Model(BaseModel):
@NotBlank(field_name='{{ column.column_name }}', message='{{ comment }}不能为空')
def get_{{ column.column_name }}(self):
return self.{{ column.column_name }}
{% if not loop.last %}{{ "\n" }}{% endif %}
{% endif %}
{% endfor %}
@@ -64,6 +63,45 @@ class {{ BusinessName }}Model(BaseModel):
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
class {{ BusinessName }}Model({% if table.sub %}{{ BusinessName }}BaseModel{% else %}BaseModel{% endif %}):
"""
{{ functionName }}表对应pydantic模型
"""
{% if not table.sub %}
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
{% for column in columns %}
{{ column.column_name }}: Optional[{{ column.python_type }}] = Field(default=None, description='{{ column.column_comment }}')
{% endfor %}
{% endif %}
{% if table.sub %}
{{ subclassName }}_list: Optional[List['{{ subTable.business_name | capitalize }}Model']] = Field(default=None, description='子表列信息')
{% endif %}
{% if not table.sub %}
{% for column in columns %}
{% if column.required %}
{% set parentheseIndex = column.column_comment.find("") %}
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
@NotBlank(field_name='{{ column.column_name }}', message='{{ comment }}不能为空')
def get_{{ column.column_name }}(self):
return self.{{ column.column_name }}
{% if not loop.last %}{{ "\n" }}{% endif %}
{% endif %}
{% endfor %}
{% if vo_field_required.has_required %}
def validate_fields(self):
{% for column in columns %}
{% if column.required %}
self.get_{{ column.column_name }}()
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% if table.sub %}
@@ -85,6 +123,7 @@ class {{ subTable.business_name | capitalize }}Model(BaseModel):
@NotBlank(field_name='{{ sub_column.column_name }}', message='{{ comment }}不能为空')
def get_{{ sub_column.column_name }}(self):
return self.{{ sub_column.column_name }}
{% if not loop.last %}{{ "\n" }}{% endif %}
{% endif %}
{% endfor %}
@@ -99,7 +138,7 @@ class {{ subTable.business_name | capitalize }}Model(BaseModel):
{% endif %}
class {{ BusinessName }}QueryModel({{ BusinessName }}Model):
class {{ BusinessName }}QueryModel({% if table.sub %}{{ BusinessName }}BaseModel{% else %}{{ BusinessName }}Model{% endif %}):
"""
{{ functionName }}不分页查询模型
"""

View File

@@ -243,7 +243,7 @@
{% else %}
:label="dict.value"
{% endif %}>
{{ dict.label }}
{% raw %}{{{% endraw %} dict.label {% raw %}}}{% endraw %}
</el-radio>
</el-radio-group>
</el-form-item>

View File

@@ -229,7 +229,7 @@
{% else %}
:label="dict.value"
{% endif %}>
{{ dict.label }}
{% raw %}{{{% endraw %} dict.label {% raw %}}}{% endraw %}
</el-radio>
</el-radio-group>
</el-form-item>
@@ -297,7 +297,7 @@
<template #default="scope">
<el-select v-model="scope.row.{{ pythonField }}" placeholder="请选择{{ comment }}">
<el-option
v-for="dict in {{ column.dictType }}"
v-for="dict in {{ column.dict_type }}"
:key="dict.value"
:label="dict.label"
:value="dict.value"

View File

@@ -243,7 +243,7 @@ class TemplateUtils:
sub_columns = gen_table.sub_table.columns or []
for sub_column in sub_columns:
if sub_column.python_type in GenConstant.TYPE_DATE:
import_list.add(f'from datetime import {column.python_type}')
import_list.add(f'from datetime import {sub_column.python_type}')
elif sub_column.python_type == GenConstant.TYPE_DECIMAL:
import_list.add('from decimal import Decimal')
return cls.merge_same_imports(list(import_list), 'from datetime import')
@@ -267,6 +267,7 @@ class TemplateUtils:
f'from sqlalchemy import {StringUtil.get_mapping_value_by_key_ignore_case(GenConstant.DB_TO_SQLALCHEMY_TYPE_MAPPING, data_type)}'
)
if gen_table.sub:
import_list.add('from sqlalchemy import ForeignKey')
sub_columns = gen_table.sub_table.columns or []
for sub_column in sub_columns:
data_type = cls.get_db_type(sub_column.column_type)