Fix database sequence errors (#4777)

* Fix database upgrade script to handle existing identity columns

Co-authored-by: jxxghp <jxxghp@live.cn>

* Improve identity column conversion with error handling and cleanup

Co-authored-by: jxxghp <jxxghp@live.cn>

* Fix database upgrade script to handle existing identity columns

Co-authored-by: jxxghp <jxxghp@live.cn>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: jxxghp <jxxghp@live.cn>
This commit is contained in:
jxxghp
2025-08-20 00:29:35 +08:00
committed by GitHub
parent 1eb85003de
commit db0ea7d6c4

View File

@@ -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