From 58165c395181755b4c6291bacb47ad5bb9e86d3b Mon Sep 17 00:00:00 2001 From: Asuka Minato Date: Tue, 26 Aug 2025 19:16:58 +0900 Subject: [PATCH] example on orm typing (#23322) --- api/core/helper/encrypter.py | 1 + api/models/account.py | 6 +++--- api/models/dataset.py | 4 ++-- api/models/model.py | 8 ++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/api/core/helper/encrypter.py b/api/core/helper/encrypter.py index f761d2037..cac7e8e6e 100644 --- a/api/core/helper/encrypter.py +++ b/api/core/helper/encrypter.py @@ -17,6 +17,7 @@ def encrypt_token(tenant_id: str, token: str): if not (tenant := db.session.query(Tenant).where(Tenant.id == tenant_id).first()): raise ValueError(f"Tenant with id {tenant_id} not found") + assert tenant.encrypt_public_key is not None encrypted_token = rsa.encrypt(token, tenant.encrypt_public_key) return base64.b64encode(encrypted_token).decode() diff --git a/api/models/account.py b/api/models/account.py index 1a0752440..7b7d39341 100644 --- a/api/models/account.py +++ b/api/models/account.py @@ -200,7 +200,7 @@ class Tenant(Base): id: Mapped[str] = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()")) name: Mapped[str] = mapped_column(String(255)) - encrypt_public_key = db.Column(sa.Text) + encrypt_public_key: Mapped[Optional[str]] = mapped_column(sa.Text) plan: Mapped[str] = mapped_column(String(255), server_default=sa.text("'basic'::character varying")) status: Mapped[str] = mapped_column(String(255), server_default=sa.text("'normal'::character varying")) custom_config: Mapped[Optional[str]] = mapped_column(sa.Text) @@ -325,5 +325,5 @@ class TenantPluginAutoUpgradeStrategy(Base): upgrade_mode: Mapped[UpgradeMode] = mapped_column(String(16), nullable=False, server_default="exclude") exclude_plugins: Mapped[list[str]] = mapped_column(sa.ARRAY(String(255)), nullable=False) # plugin_id (author/name) include_plugins: Mapped[list[str]] = mapped_column(sa.ARRAY(String(255)), nullable=False) # plugin_id (author/name) - created_at = db.Column(DateTime, nullable=False, server_default=func.current_timestamp()) - updated_at = db.Column(DateTime, nullable=False, server_default=func.current_timestamp()) + created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp()) + updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp()) diff --git a/api/models/dataset.py b/api/models/dataset.py index 1714d29e7..d4519f62d 100644 --- a/api/models/dataset.py +++ b/api/models/dataset.py @@ -62,8 +62,8 @@ class Dataset(Base): created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp()) updated_by = mapped_column(StringUUID, nullable=True) updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp()) - embedding_model = db.Column(String(255), nullable=True) # TODO: mapped_column - embedding_model_provider = db.Column(String(255), nullable=True) # TODO: mapped_column + embedding_model = mapped_column(String(255), nullable=True) + embedding_model_provider = mapped_column(String(255), nullable=True) collection_binding_id = mapped_column(StringUUID, nullable=True) retrieval_model = mapped_column(JSONB, nullable=True) built_in_field_enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) diff --git a/api/models/model.py b/api/models/model.py index c4303f3cc..aeb2cad62 100644 --- a/api/models/model.py +++ b/api/models/model.py @@ -77,7 +77,7 @@ class App(Base): description: Mapped[str] = mapped_column(sa.Text, server_default=sa.text("''::character varying")) mode: Mapped[str] = mapped_column(String(255)) icon_type: Mapped[Optional[str]] = mapped_column(String(255)) # image, emoji - icon = db.Column(String(255)) + icon = mapped_column(String(255)) icon_background: Mapped[Optional[str]] = mapped_column(String(255)) app_model_config_id = mapped_column(StringUUID, nullable=True) workflow_id = mapped_column(StringUUID, nullable=True) @@ -904,7 +904,7 @@ class Message(Base): message_tokens: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0")) message_unit_price = mapped_column(sa.Numeric(10, 4), nullable=False) message_price_unit = mapped_column(sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001")) - answer: Mapped[str] = db.Column(sa.Text, nullable=False) # TODO make it mapped_column + answer: Mapped[str] = mapped_column(sa.Text, nullable=False) answer_tokens: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0")) answer_unit_price = mapped_column(sa.Numeric(10, 4), nullable=False) answer_price_unit = mapped_column(sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001")) @@ -1321,7 +1321,7 @@ class MessageAnnotation(Base): app_id: Mapped[str] = mapped_column(StringUUID) conversation_id: Mapped[Optional[str]] = mapped_column(StringUUID, sa.ForeignKey("conversations.id")) message_id: Mapped[Optional[str]] = mapped_column(StringUUID) - question = db.Column(sa.Text, nullable=True) + question = mapped_column(sa.Text, nullable=True) content = mapped_column(sa.Text, nullable=False) hit_count: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0")) account_id = mapped_column(StringUUID, nullable=False) @@ -1677,7 +1677,7 @@ class MessageAgentThought(Base): message_unit_price = mapped_column(sa.Numeric, nullable=True) message_price_unit = mapped_column(sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001")) message_files = mapped_column(sa.Text, nullable=True) - answer = db.Column(sa.Text, nullable=True) + answer = mapped_column(sa.Text, nullable=True) answer_token: Mapped[Optional[int]] = mapped_column(sa.Integer, nullable=True) answer_unit_price = mapped_column(sa.Numeric, nullable=True) answer_price_unit = mapped_column(sa.Numeric(10, 7), nullable=False, server_default=sa.text("0.001"))