From 4184d8c7acf7371e163bbda1cb02444f367a6bdc Mon Sep 17 00:00:00 2001 From: Carol Date: Fri, 22 Aug 2025 10:55:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E8=BF=81=E7=A7=BB=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=BC=82=E5=B8=B8=E7=9A=84=E6=B3=A8=E6=84=8F?= =?UTF-8?q?=E4=BA=8B=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add: sqlite迁移到postgresql的注意事项 --- docs/postgresql-setup.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/postgresql-setup.md b/docs/postgresql-setup.md index 107f0e63..6b6a7570 100644 --- a/docs/postgresql-setup.md +++ b/docs/postgresql-setup.md @@ -122,6 +122,36 @@ DB_POSTGRESQL_PASSWORD=your-password 3. 启动应用,数据库表会自动创建 4. 使用数据库迁移工具或手动导入数据 +#### 注意事项 +完成数据迁移后需要对postgresql中的表进行索引初始值进行更新,否则会出现唯一索引已存在的异常 +例如: +```json +【EventType.SiteUpdated 事件处理出错】 + +SiteChain.cache_site_userdata +(psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "siteuserdata_pkey" +DETAIL: Key (id)=(18) already exists. + +[SQL: INSERT INTO siteuserdata (domain, name, username, userid, user_level, join_at, bonus, upload, download, ratio, seeding, leeching, seeding_size, leeching_size, seeding_info, message_unread, message_unread_contents, err_msg, updated_day, updated_time) VALUES (%(domain)s, %(name)s, %(username)s, %(userid)s, %(user_level)s, %(join_at)s, %(bonus)s, %(upload)s, %(download)s, %(ratio)s, %(seeding)s, %(leeching)s, %(seeding_size)s, %(leeching_size)s, %(seeding_info)s::JSON, %(message_unread)s, %(message_unread_contents)s::JSON, %(err_msg)s, %(updated_day)s, %(updated_time)s) RETURNING siteuserdata.id] +[parameters: {'domain': 'btschool.club', 'name': '学校', 'username': None, 'userid': None, 'user_level': None, 'join_at': None, 'bonus': 0.0, 'upload': 0, 'download': 0, 'ratio': 0.0, 'seeding': 0, 'leeching': 0, 'seeding_size': 0, 'leeching_size': 0, 'seeding_info': '[]', 'message_unread': 0, 'message_unread_contents': '[]', 'err_msg': '未检测到已登陆,请检查cookies是否过期', 'updated_day': '2025-08-22', 'updated_time': '09:52:01'}] +(Background on this error at: https://sqlalche.me/e/20/gkpj) +``` + +需要对每一个表分别执行下面的语句(下面的SQL以`workflowc`数据表为例,每张表请自行修改,其中`user`表因为关键字原因,应该写成`public.user`的方式): + +```sql +DO $$ +DECLARE + max_id INTEGER; +BEGIN + -- 查询最大 ID 值 + SELECT COALESCE(MAX(id), 0) INTO max_id FROM workflow; + + -- 调整序列 + EXECUTE format('ALTER SEQUENCE workflow_id_seq RESTART WITH %s', max_id + 1); +END $$; +``` + ### 从 PostgreSQL 迁移到 SQLite 1. 导出 PostgreSQL 数据