Skip to content

Commit 4bb7186

Browse files
committed
fix(graphrag): use ON CONFLICT instead of INSERT OR REPLACE so Postgres adapters can persist
1 parent b847096 commit 4bb7186

1 file changed

Lines changed: 50 additions & 9 deletions

File tree

src/cognition/memory/retrieval/graph/graphrag/GraphRAGEngine.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,12 +1804,32 @@ Provide a comprehensive answer based on the information above.`,
18041804
);
18051805
}
18061806

1807-
// Persist entities
1807+
// Persist entities.
1808+
//
1809+
// `INSERT OR REPLACE` is SQLite-only; Postgres chokes with
1810+
// "syntax error at or near OR" at position 8 (right after
1811+
// "INSERT "). Previously every wilds-ai compile against the
1812+
// production Postgres adapter logged `graphrag_seed_failed`,
1813+
// silently dropping GraphRAG seeds for every new blueprint.
1814+
// The `INSERT ... ON CONFLICT (id) DO UPDATE SET ...` form is
1815+
// portable across Postgres + SQLite >= 3.24 (default since 2018),
1816+
// and the `EXCLUDED.<col>` reference pulls the proposed insert
1817+
// value without re-binding parameters.
18081818
for (const entity of this.entities.values()) {
18091819
await this.persistenceAdapter.run(
1810-
`INSERT OR REPLACE INTO ${this.tablePrefix}entities
1820+
`INSERT INTO ${this.tablePrefix}entities
18111821
(id, name, type, description, properties_json, embedding_json, source_document_ids_json, frequency, created_at, updated_at)
1812-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1822+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1823+
ON CONFLICT (id) DO UPDATE SET
1824+
name = EXCLUDED.name,
1825+
type = EXCLUDED.type,
1826+
description = EXCLUDED.description,
1827+
properties_json = EXCLUDED.properties_json,
1828+
embedding_json = EXCLUDED.embedding_json,
1829+
source_document_ids_json = EXCLUDED.source_document_ids_json,
1830+
frequency = EXCLUDED.frequency,
1831+
created_at = EXCLUDED.created_at,
1832+
updated_at = EXCLUDED.updated_at`,
18131833
[
18141834
entity.id,
18151835
entity.name,
@@ -1825,12 +1845,22 @@ Provide a comprehensive answer based on the information above.`,
18251845
);
18261846
}
18271847

1828-
// Persist relationships
1848+
// Persist relationships (see entity comment above for the
1849+
// INSERT OR REPLACE → ON CONFLICT migration rationale).
18291850
for (const rel of this.relationships.values()) {
18301851
await this.persistenceAdapter.run(
1831-
`INSERT OR REPLACE INTO ${this.tablePrefix}relationships
1852+
`INSERT INTO ${this.tablePrefix}relationships
18321853
(id, source_entity_id, target_entity_id, type, description, weight, properties_json, source_document_ids_json, created_at)
1833-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1854+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1855+
ON CONFLICT (id) DO UPDATE SET
1856+
source_entity_id = EXCLUDED.source_entity_id,
1857+
target_entity_id = EXCLUDED.target_entity_id,
1858+
type = EXCLUDED.type,
1859+
description = EXCLUDED.description,
1860+
weight = EXCLUDED.weight,
1861+
properties_json = EXCLUDED.properties_json,
1862+
source_document_ids_json = EXCLUDED.source_document_ids_json,
1863+
created_at = EXCLUDED.created_at`,
18341864
[
18351865
rel.id,
18361866
rel.sourceEntityId,
@@ -1845,12 +1875,23 @@ Provide a comprehensive answer based on the information above.`,
18451875
);
18461876
}
18471877

1848-
// Persist communities
1878+
// Persist communities (see entity comment above).
18491879
for (const community of this.communities.values()) {
18501880
await this.persistenceAdapter.run(
1851-
`INSERT OR REPLACE INTO ${this.tablePrefix}communities
1881+
`INSERT INTO ${this.tablePrefix}communities
18521882
(id, level, parent_community_id, child_community_ids_json, entity_ids_json, relationship_ids_json, summary, findings_json, importance, title, created_at)
1853-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1883+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1884+
ON CONFLICT (id) DO UPDATE SET
1885+
level = EXCLUDED.level,
1886+
parent_community_id = EXCLUDED.parent_community_id,
1887+
child_community_ids_json = EXCLUDED.child_community_ids_json,
1888+
entity_ids_json = EXCLUDED.entity_ids_json,
1889+
relationship_ids_json = EXCLUDED.relationship_ids_json,
1890+
summary = EXCLUDED.summary,
1891+
findings_json = EXCLUDED.findings_json,
1892+
importance = EXCLUDED.importance,
1893+
title = EXCLUDED.title,
1894+
created_at = EXCLUDED.created_at`,
18541895
[
18551896
community.id,
18561897
community.level,

0 commit comments

Comments
 (0)