Skip to content

Commit db0ea7d

Browse files
jxxghpcursoragentjxxghp
authored
Fix database sequence errors (jxxghp#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>
1 parent 1eb8500 commit db0ea7d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

database/versions/5b3355c964bb_2_2_0.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def fix_table_sequence(connection, table_name):
5959

6060
# 检查表是否有id列
6161
result = connection.execute(sa.text(f"""
62-
SELECT column_name, data_type, is_nullable, column_default
62+
SELECT is_identity, column_default
6363
FROM information_schema.columns
6464
WHERE table_name = '{table_name}'
6565
AND column_name = 'id'
@@ -70,10 +70,10 @@ def fix_table_sequence(connection, table_name):
7070
print(f"表 {table_name} 没有id列,跳过")
7171
return
7272

73-
_, _, _, column_default = id_column
73+
is_identity, column_default = id_column
7474

7575
# 检查是否已经是Identity类型
76-
if column_default and 'GENERATED BY DEFAULT AS IDENTITY' in column_default:
76+
if is_identity == 'YES' or (column_default and 'GENERATED BY DEFAULT AS IDENTITY' in column_default):
7777
print(f"表 {table_name} 的id列已经是Identity类型,跳过")
7878
return
7979

@@ -110,4 +110,8 @@ def convert_to_identity(connection, table_name):
110110

111111
except Exception as e:
112112
print(f"转换表 {table_name} 序列时出错: {e}")
113+
# 如果是已经存在的Identity错误,则忽略
114+
if "already an identity column" in str(e):
115+
print(f"表 {table_name} 的id列已经是Identity类型,忽略此错误")
116+
return
113117
raise

0 commit comments

Comments
 (0)