zhoujiaangyao commited on
Commit ·
b98be44
1
Parent(s): 6cfe55f
fix: models.provider_id 用 String,迁移 Postgres 整型旧列
Browse files- backend/app/db/init_db.py +23 -0
- backend/app/db/models/models.py +4 -1
backend/app/db/init_db.py
CHANGED
|
@@ -15,6 +15,7 @@ def init_db():
|
|
| 15 |
|
| 16 |
Base.metadata.create_all(bind=engine)
|
| 17 |
_ensure_article_content_text(engine)
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# 注:原 _ensure_model_columns 为 models.supports_multimodal 做的迁移已删除——
|
|
@@ -23,6 +24,28 @@ def init_db():
|
|
| 23 |
# 已有 SQLite 库里残留的该列无害,保持不动即可。
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def _ensure_article_content_text(engine):
|
| 27 |
inspector = inspect(engine)
|
| 28 |
if "article_items" not in inspector.get_table_names():
|
|
|
|
| 15 |
|
| 16 |
Base.metadata.create_all(bind=engine)
|
| 17 |
_ensure_article_content_text(engine)
|
| 18 |
+
_ensure_model_provider_id_text(engine)
|
| 19 |
|
| 20 |
|
| 21 |
# 注:原 _ensure_model_columns 为 models.supports_multimodal 做的迁移已删除——
|
|
|
|
| 24 |
# 已有 SQLite 库里残留的该列无害,保持不动即可。
|
| 25 |
|
| 26 |
|
| 27 |
+
def _ensure_model_provider_id_text(engine):
|
| 28 |
+
# 早期 Postgres 部署里 models.provider_id 被建成 INTEGER(ORM 旧定义),
|
| 29 |
+
# 但实际存的是字符串 provider id(如 "deepseek"),导致查询报
|
| 30 |
+
# invalid input syntax for type integer。这里把已有的整型列就地改成 VARCHAR。
|
| 31 |
+
# 仅 Postgres 需要;SQLite 动态类型无此问题。幂等(改完后类型名不含 INT 即跳过)。
|
| 32 |
+
if engine.dialect.name != "postgresql":
|
| 33 |
+
return
|
| 34 |
+
inspector = inspect(engine)
|
| 35 |
+
if "models" not in inspector.get_table_names():
|
| 36 |
+
return
|
| 37 |
+
for column in inspector.get_columns("models"):
|
| 38 |
+
if column["name"] != "provider_id":
|
| 39 |
+
continue
|
| 40 |
+
if "INT" in str(column["type"]).upper():
|
| 41 |
+
with engine.begin() as conn:
|
| 42 |
+
conn.execute(text(
|
| 43 |
+
"ALTER TABLE models ALTER COLUMN provider_id TYPE VARCHAR "
|
| 44 |
+
"USING provider_id::varchar"
|
| 45 |
+
))
|
| 46 |
+
break
|
| 47 |
+
|
| 48 |
+
|
| 49 |
def _ensure_article_content_text(engine):
|
| 50 |
inspector = inspect(engine)
|
| 51 |
if "article_items" not in inspector.get_table_names():
|
backend/app/db/models/models.py
CHANGED
|
@@ -7,6 +7,9 @@ class Model(Base):
|
|
| 7 |
__tablename__ = "models"
|
| 8 |
|
| 9 |
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 10 |
-
provider_id
|
|
|
|
|
|
|
|
|
|
| 11 |
model_name = Column(String, nullable=False)
|
| 12 |
created_at = Column(DateTime, server_default=func.now())
|
|
|
|
| 7 |
__tablename__ = "models"
|
| 8 |
|
| 9 |
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 10 |
+
# provider_id 存的是 Provider.id(字符串,如 "deepseek"),必须用 String。
|
| 11 |
+
# 旧版误写成 Integer,SQLite 动态类型未暴露问题,但 Postgres 严格校验会报
|
| 12 |
+
# invalid input syntax for type integer: "deepseek"。见 init_db 的列类型迁移。
|
| 13 |
+
provider_id = Column(String, nullable=False)
|
| 14 |
model_name = Column(String, nullable=False)
|
| 15 |
created_at = Column(DateTime, server_default=func.now())
|