From db0ea7d6c48b011640d82b8893a30433702a6d10 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Wed, 20 Aug 2025 00:29:35 +0800 Subject: [PATCH] Fix database sequence errors (#4777) * Fix database upgrade script to handle existing identity columns Co-authored-by: jxxghp * Improve identity column conversion with error handling and cleanup Co-authored-by: jxxghp * Fix database upgrade script to handle existing identity columns Co-authored-by: jxxghp --------- Co-authored-by: Cursor Agent Co-authored-by: jxxghp --- database/versions/5b3355c964bb_2_2_0.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/database/versions/5b3355c964bb_2_2_0.py b/database/versions/5b3355c964bb_2_2_0.py index 4d4aebef..4248810d 100644 --- a/database/versions/5b3355c964bb_2_2_0.py +++ b/database/versions/5b3355c964bb_2_2_0.py @@ -59,7 +59,7 @@ def fix_table_sequence(connection, table_name): # 检查表是否有id列 result = connection.execute(sa.text(f""" - SELECT column_name, data_type, is_nullable, column_default + SELECT is_identity, column_default FROM information_schema.columns WHERE table_name = '{table_name}' AND column_name = 'id' @@ -70,10 +70,10 @@ def fix_table_sequence(connection, table_name): print(f"表 {table_name} 没有id列,跳过") return - _, _, _, column_default = id_column + is_identity, column_default = id_column # 检查是否已经是Identity类型 - if column_default and 'GENERATED BY DEFAULT AS IDENTITY' in column_default: + if is_identity == 'YES' or (column_default and 'GENERATED BY DEFAULT AS IDENTITY' in column_default): print(f"表 {table_name} 的id列已经是Identity类型,跳过") return @@ -110,4 +110,8 @@ def convert_to_identity(connection, table_name): except Exception as e: print(f"转换表 {table_name} 序列时出错: {e}") + # 如果是已经存在的Identity错误,则忽略 + if "already an identity column" in str(e): + print(f"表 {table_name} 的id列已经是Identity类型,忽略此错误") + return raise