63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
|
|
"""手动添加 baodan_conversation_id 字段到会话表"""
|
||
|
|
import psycopg2
|
||
|
|
|
||
|
|
# 数据库连接配置
|
||
|
|
DB_CONFIG = {
|
||
|
|
"host": "localhost",
|
||
|
|
"port": 5432,
|
||
|
|
"database": "baodan",
|
||
|
|
"user": "postgres",
|
||
|
|
"password": "taiyi1224",
|
||
|
|
}
|
||
|
|
|
||
|
|
def add_column():
|
||
|
|
"""添加 baodan_conversation_id 字段。"""
|
||
|
|
conn = None
|
||
|
|
try:
|
||
|
|
# 连接数据库
|
||
|
|
conn = psycopg2.connect(**DB_CONFIG)
|
||
|
|
cur = conn.cursor()
|
||
|
|
|
||
|
|
# 检查字段是否已存在
|
||
|
|
cur.execute("""
|
||
|
|
SELECT column_name
|
||
|
|
FROM information_schema.columns
|
||
|
|
WHERE table_name = 'insurance_chat_sessions'
|
||
|
|
AND column_name = 'baodan_conversation_id'
|
||
|
|
""")
|
||
|
|
if cur.fetchone():
|
||
|
|
print("字段 baodan_conversation_id 已存在,跳过添加")
|
||
|
|
return
|
||
|
|
|
||
|
|
# 添加新字段
|
||
|
|
cur.execute("""
|
||
|
|
ALTER TABLE insurance_chat_sessions
|
||
|
|
ADD COLUMN baodan_conversation_id VARCHAR(128) NULL
|
||
|
|
""")
|
||
|
|
conn.commit()
|
||
|
|
|
||
|
|
print("成功添加 baodan_conversation_id 字段")
|
||
|
|
|
||
|
|
# 验证字段已添加
|
||
|
|
cur.execute("""
|
||
|
|
SELECT column_name, data_type, is_nullable
|
||
|
|
FROM information_schema.columns
|
||
|
|
WHERE table_name = 'insurance_chat_sessions'
|
||
|
|
AND column_name = 'baodan_conversation_id'
|
||
|
|
""")
|
||
|
|
result = cur.fetchone()
|
||
|
|
if result:
|
||
|
|
print(f"验证成功: 字段={result[0]}, 类型={result[1]}, 可为空={result[2]}")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"添加字段失败: {e}")
|
||
|
|
if conn:
|
||
|
|
conn.rollback()
|
||
|
|
raise
|
||
|
|
finally:
|
||
|
|
if conn:
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
add_column()
|