From 3bae5b2f5c1a5c6960083db0eb039749f2b297d4 Mon Sep 17 00:00:00 2001 From: Echo-Note Date: Thu, 22 Oct 2026 11:57:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E8=B7=A8=E6=96=B9?= =?UTF-8?q?=E8=A8=80=20JSON=20=E7=B1=BB=E5=9E=8B=20JSONType=EF=BC=88PG=20?= =?UTF-8?q?=E7=94=A8=20JSONB=EF=BC=8CSQLite/MySQL=20=E9=99=8D=E7=BA=A7=20J?= =?UTF-8?q?SON=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/core/base_model.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/app/core/base_model.py b/backend/app/core/base_model.py index 339494b4c..459406839 100644 --- a/backend/app/core/base_model.py +++ b/backend/app/core/base_model.py @@ -1,6 +1,7 @@ from datetime import UTC, datetime -from sqlalchemy import Boolean, DateTime, ForeignKey, Index, Integer, MetaData, String +from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, Index, Integer, MetaData, String +from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.ext.asyncio import AsyncAttrs from sqlalchemy.orm import DeclarativeBase, Mapped, declared_attr, mapped_column, relationship @@ -18,6 +19,12 @@ "pk": "pk_%(table_name)s", } +# 跨方言 JSON 类型:PostgreSQL(生产)使用 JSONB,SQLite/MySQL(测试环境)降级为 +# 通用 JSON,保证 create_all 与 autogenerate 在三种方言下均可渲染、零漂移。 +# 各模块定义 JSON 列时统一使用 JSONType,而非直接硬编码 JSONB,避免破坏基类 +# 「兼容 SQLite、MySQL 和 PostgreSQL」的承诺。 +JSONType = JSONB().with_variant(JSON(), "sqlite", "mysql") + class MappedBase(AsyncAttrs, DeclarativeBase): """声明式基类