Skip to content

✨ feat(sqlite): 支持表结构编辑器配置自增主键 - #8949

Merged
t8y2 merged 4 commits into
t8y2:mainfrom
eryajf:issue_8937
Sep 12, 2026
Merged

✨ feat(sqlite): 支持表结构编辑器配置自增主键#8949
t8y2 merged 4 commits into
t8y2:mainfrom
eryajf:issue_8937

Conversation

@eryajf

@eryajf eryajf commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

变更说明

为 SQLite 表结构编辑器增加自增主键配置能力。

现在可以在字段扩展属性中为整型主键启用“自增”,并在重新打开已有表时恢复 AUTOINCREMENT 状态。生成 SQLite DDL 时使用合法的内联语法:

"aa" INTEGER PRIMARY KEY AUTOINCREMENT

同时支持 INTEGERINTTINYINTSMALLINTMEDIUMINTBIGINT 等 SQLite 整型别名。

变更类型

  • 新功能
  • Bug 修复
  • 性能优化
  • 代码重构
  • 文档更新
  • CI / 构建

涉及前端

  • 本 PR 涉及前端改动,已附截图/录屏(见下方)
image

验证

已手动验证:

  1. 创建 SQLite 表。
  2. 添加 INTEGER 主键字段并启用“自增”。
  3. 插入两条只填写其他字段的数据。
  4. 查询结果中主键自动生成为 12

关联 Issue

Close #8937

- 新增 SQLite 整数类型主键的 AUTOINCREMENT 选项,并限制单表仅一个自增主键
- 解析 SQLite `AUTOINCREMENT` 列属性并保留扩展属性
- 生成 SQLite 建表语句时,将 `PRIMARY KEY AUTOINCREMENT` 内联至列定义
- 避免非自增整数主键自动附加 AUTOINCREMENT
@github-actions github-actions Bot added area/core Shared DBX core runtime area/desktop Desktop application or Tauri shell enhancement New feature or request ui-change Changes user-visible interface, text, or visual assets labels Sep 12, 2026
eryajf and others added 2 commits September 12, 2026 21:51
- apply_auto_inc_to_column_def only emits the AUTOINCREMENT suffix when
  the source column explicitly requests auto-increment (the implicit
  integer-PK heuristic keeps applying to other suffixes), so schema
  sync and rebuild no longer upgrade plain or composite integer
  primary keys to sqlite_sequence semantics or emit invalid DDL
- recognize the native autoincrement spelling when reading back
  metadata, and normalize integer-family aliases to exact INTEGER in
  both the create-table and sync paths since SQLite accepts
  AUTOINCREMENT only on INTEGER PRIMARY KEY
- reject composite or non-integer auto-increment primary keys with a
  clear warning, and surface an explicit rebuild warning when toggling
  AUTOINCREMENT on an existing column instead of silently no-oping
@t8y2

t8y2 commented Sep 12, 2026

Copy link
Copy Markdown
Owner

感谢贡献!整体方向正确(内联 PRIMARY KEY AUTOINCREMENT、抑制表级 PK,与 DBeaver 行为一致)。我在 3abb735a 修复了两个正确性问题:

  1. 别名类型会生成非法 DDLisSqliteIntegerType 放行的 int/tinyint/smallint/mediumint/bigint 勾选自增后会生成 INT PRIMARY KEY AUTOINCREMENT,而 SQLite 只接受精确的 INTEGER PRIMARY KEY AUTOINCREMENT;复合主键 + 自增还会生成两处 PRIMARY KEY。补丁在 create 路径把自增列类型归一为 INTEGER,并用校验拦截复合主键/非整数类型的自增勾选。
  2. AUTOINCREMENT 后缀被无条件附加到同步 DDLddl_profile 改为 Suffix(" AUTOINCREMENT") 后,apply_auto_inc_to_column_def 的 Suffix 分支只看 is_primary_key && is_integer_like,并不读取 wants_auto——原提交里 wants_auto 的排除对该分支是死代码。后果是跨库同步/回滚重建到 SQLite 时,非自增整型主键被静默升级为 AUTOINCREMENT(sqlite_sequence/行号不复用),复合整型主键直接生成非法 DDL。补丁给该后缀加了显式意图门控(源列明确自增才附加;MySQL 等其他后缀的既有隐式行为不变),column_is_auto_increment 兼容无下划线的 autoincrement 拼写,同步路径同样把整型别名归一为 INTEGER
  3. 已有表上切换"自增"原来保存时是静默 no-op,现在会给出明确的重建警告(未改动的列继承原状态,不误报)。

新增 6 个回归测试(别名归一、复合/非整数拒绝、普通整型 PK 同步不升级、复合 PK 同步合法、显式自增保留且 BIGINT 归一),table_structure_sql 299 例 + schema_diff 251 例全过,生成的 DDL 已用本机 sqlite3 内存库逐条验证合法。#8937 中"修改已有表自增"的部分(需要重建表)仍留待后续,Issue 保持打开。

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direction verified against DBeaver's SQLite editor behavior (inline PRIMARY KEY AUTOINCREMENT, suppressed table-level PK), and the follow-up patch closed the alias-type DDL, the unconditional sync-path AUTOINCREMENT suffix, and the silent no-op on existing tables, with six regression tests and real-sqlite3 validation. All checks green; merging. The existing-table rebuild half of #8937 stays open for follow-up.

@t8y2
t8y2 merged commit 3767f4a into t8y2:main Sep 12, 2026
14 checks passed
@t8y2

t8y2 commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution! Merged in 3767f4a, will be released in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Shared DBX core runtime area/desktop Desktop application or Tauri shell enhancement New feature or request ui-change Changes user-visible interface, text, or visual assets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] sqlite好像没有能设置主键自增的功能

2 participants