sqlite-schema-design
它能做什么
这份指南讲解为 SQLite Sync 设计数据表。SQLite Sync 是一个让 SQLite 数据库在多台设备之间保持同步的系统。适合开发同步应用的开发者,他们需要的是同步规则,而不是通用的数据库建议。
适用场景
- ✓当你要新增一个需要跨设备同步的数据表时。
- ✓当你修改已经同步的数据表中的某一列时。
- ✓当你规划的数据库迁移需要与 SQLite Sync 兼容时。
技能链接
https://github.com/fastrepl/anarlog/blob/main/.agents/skills/sqlite-schema-design/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请用 SQLite Sync 设计一张笔记表,让用户的各台设备之间保持同步。”
每周 5 个新技能,1 分钟读完。
工作原理
这个技能会先检查某个表是否应当同步,然后应用同步规则。这些规则包括:使用由同步 UUID 助手生成的稳定文本主键,不使用自增整数 id,以及通过同步的 alter 步骤进行架构变更。本地和云端数据库必须保持相同的架构。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Goal
Design tables that behave correctly under SQLite Sync's CRDT replication model.
This skill is specifically for CloudSync-backed schemas:
- tables are initialized through
cloudsync_init(...) - sync is enabled with
cloudsync_enable(...) - identifiers should be generated with
cloudsync_uuid() - schema changes must go through
cloudsync_begin_alter(...)andcloudsync_commit_alter(...) - local and cloud databases must keep the same schema
Do not treat this as ordinary SQLite schema design. SQLite Sync imposes extra rules around keys, defaults, foreign keys, and schema evolution.
Workflow
1. Decide Whether The Table Is Synced
Before proposing DDL, classify the table:
- synced application data: must satisfy SQLite Sync constraints
- local-only cache or ephemeral state: should usually stay out of CloudSync
Only apply this skill to synced tables or to tables that may become synced soon.
2. Require A Stable, Globally Unique Primary Key
For synced tables:
- always declare an explicit primary key
- prefer
TEXT PRIMARY KEY NOT NULL - generate ids with
cloudsync_uuid() - do not use auto-incrementing integer ids
SQLite Sync docs explicitly recommend UUIDv7-style globally unique ids for CRDT workloads. Integer autoincrement ids are a bad fit because multiple devices can create rows independently.
CREATE TABLE document (
id TEXT PRIMARY KEY NOT NULL DEFAULT (cloudsync_uuid()),
workspace_id TEXT NOT NULL,
title TEXT NOT NULL DEFAULT '',
body TEXT NOT NULL DEFAULT '',
archived INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
) STRICT;
If the environment cannot use a function call in DEFAULT, generate the id in application code, but still use cloudsync_uuid() as the canonical id strategy.
3. Make Inserts Merge-Safe With Real Defaults
SQLite Sync best practices call out a non-obvious CRDT constraint: merges can happen column-by-column, so missing values are much more dangerous than in a single-node SQLite app.
For synced tables:
- every non-primary-key
NOT NULLcolumn should have a meaningfulDEFAULT - avoid required columns that only application code knows how to populate
- prefer simple scalar defaults over nullable columns when the field is logically always present
Good:
title TEXT NOT NULL DEFAULT ''
archived INTEGER NOT NULL DEFAULT 0
sort_order INTEGER NOT NULL DEFAULT 0
Bad:
title TEXT NOT NULL
archived INTEGER NOT NULL
without defaults on a synced table.
4. Keep The Local And Cloud Schemas Identical
The getting-started docs require the local synced database and the SQLite Cloud database to share the same schema.
When designing or reviewing a schema:
- treat local and remote DDL as one contract
- do not introduce "client-only" columns on synced tables
- do not rely on drift being harmless
- ensure migrations are applied consistently before sync resumes
If a field is only needed locally, it likely belongs in a separate non-synced table.
5. Be Conservative With Foreign Keys
SQLite Sync best practices explicitly warn that foreign keys can interact poorly with CRDT replication.
Use foreign keys on synced tables only when the integrity guarantee is worth the operational cost.
If you keep them:
- make child foreign key columns nullable when the relationship is optional
- if a foreign key column has a
DEFAULT, that default must beNULLor reference an actually valid parent row - avoid fake sentinel ids such as
'root'unless that parent row is guaranteed to exist everywhere - index the child foreign key columns
Prefer ownership patterns that tolerate out-of-order arrival between related rows.
6. Avoid Triggers And Implicit Write Logic On Synced Tables
SQLite Sync best practices advise minimizing triggers because they make replicated writes harder to reason about.
For synced tables:
- avoid triggers that mutate synced columns
- avoid hidden side effects on insert or update
- prefer explicit application writes
- keep derived or bookkeeping writes in non-synced tables if possible
If a trigger is unavoidable, review it as part of the replication design, not as a local SQLite convenience.
7. Scope Uniqueness For RLS And Multi-Tenant Sync
The introduction docs emphasize row-level security and multi-tenant access patterns.
That changes uniqueness design:
- if the real rule is "unique per workspace/user/team", enc
常见问题
为什么不用自增 id?
多台设备可能同时创建行,整数计数器会产生冲突。因此指南要求使用全局唯一的 id。
这适用于每一张表吗?
不是。只有需要同步的表才需要这些规则,仅限本地的缓存表通常不参与同步。
我该如何修改已同步的表?
修改需要通过 begin 和 commit 这两个 alter 步骤进行,以保证架构在各处保持一致。
详情
作者的原始描述
Design or review schemas for `crates/cloudsync` using SQLite Sync constraints, not generic SQLite advice. Use when adding synced tables, changing synced columns, or planning CloudSync-safe migrations.
repo: fastrepl/anarlog
热度
- GitHub 星标
- 9.7k
- 排名
- #12214
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …