electric-sql / electric-sql/electric
Duplicate unique key "todos_pkey" in write patterns example
- Dominant language
- TypeScript
- Stars
- 10.4k
- Forks
- 375
- Avg merge
- 3d 1h
- Merged PRs (30d)
- 18
Description
**Versions**
- git commit 9bc859fb2f40c764c8009e3c0bbd4dbbdb727143
**Bug description**
Using two browsers, the following operations will crash the API.
1. open two chrome browsers, both open the write-patterns example page.
2. in browser 1's Developer Mode, Network tab, emulate Offline.
3. add an entry in 4-through-the-db section.
4. in browser 2, refresh the page.
5. in browser 1's Developer Mode, Network tab, switch back to "No throttling".
6. refresh browser 1's page.
7. check vite dev server. it should now be crashed. adding new entries in browser 1 will have no response.
```
gabriel@isoleucine 4-through-the-db % pnpm dev
> @electric-examples/write-patterns@0.0.1 dev /Users/gabriel/Desktop/electric/examples/write-patterns
> concurrently "vite" "node shared/backend/api.js"
[1] Server listening at port 3001
[0]
[0] VITE v5.4.10 ready in 91 ms
[0]
[0] ➜ Local: http://localhost:5173/
[0] ➜ Network: use --host to expose
[1] /Users/gabriel/Desktop/electric/node_modules/.pnpm/pg-pool@3.7.0_pg@8.13.1/node_modules/pg-pool/index.js:45
[1] Error.captureStackTrace(err)
[1] ^
[1]
[1] error: duplicate key value violates unique constraint "todos_pkey"
[1] at /Users/gabriel/Desktop/electric/node_modules/.pnpm/pg-pool@3.7.0_pg@8.13.1/node_modules/pg-pool/index.js:45:11
[1] at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[1] at async createTodo (file:///Users/gabriel/Desktop/electric/examples/write-patterns/shared/backend/api.js:44:3) {
[1] length: 215,
[1] severity: 'ERROR',
[1] code: '23505',
[1] detail: 'Key (id)=(77776b52-72ab-4917-8c45-e3a103dffda4) already exists.',
[1] hint: undefined,
[1] position: undefined,
[1] internalPosition: undefined,
[1] internalQuery: undefined,
[1] where: undefined,
[1] schema: 'public',
[1] table: 'todos',
[1] column: undefined,
[1] dataType: undefined,
[1] constraint: 'todos_pkey',
[1] file: 'nbtinsert.c',
[1] line: '666',
[1] routine: '_bt_check_unique'
[1] }
[1]
[1] Node.js v22.16.0
[1] node shared/backend/api.js exited with code 1
```
**Expected behavior**
Should not crash.
**Possible solution**
```
diff --git a/examples/write-patterns/shared/backend/api.js b/examples/write-patterns/shared/backend/api.js
index b742f502..04cbdf3b 100644
--- a/examples/write-patterns/shared/backend/api.js
+++ b/examples/write-patterns/shared/backend/api.js
@@ -37,6 +37,7 @@ const createTodo = async (id, title, created_at, write_id) => {
const sql = `
INSERT INTO todos (id, title, completed, created_at, write_id)
VALUES ($1, $2, false, $3, $4)
+ ON CONFLICT DO NOTHING
```
I'm very new to this framework, and not understanding the project in-depth.
Thus I'm not creating a pull request yet.
Adding this line will make the crash go away,
but I might be ignorant to the side effects it creates.
Contributor guide
Assessment
This issue has not been assessed yet.