diff --git a/db.go b/db.go index b4c1d33..bf50e40 100644 --- a/db.go +++ b/db.go @@ -116,11 +116,15 @@ CREATE TABLE IF NOT EXISTS "relationships" ( "to_person_id" INTEGER, "to_item_id" INTEGER, "directed" BOOLEAN, -- if false, the edge goes both ways - "label" TEXT, + "label" TEXT NOT NULL, FOREIGN KEY ("from_item_id") REFERENCES "items"("id") ON DELETE CASCADE, FOREIGN KEY ("to_item_id") REFERENCES "items"("id") ON DELETE CASCADE, FOREIGN KEY ("from_person_id") REFERENCES "persons"("id") ON DELETE CASCADE, - FOREIGN KEY ("to_person_id") REFERENCES "persons"("id") ON DELETE CASCADE + FOREIGN KEY ("to_person_id") REFERENCES "persons"("id") ON DELETE CASCADE, + UNIQUE ("from_item_id", "to_item_id", "label"), + UNIQUE ("from_person_id", "to_person_id", "label"), + UNIQUE ("from_item_id", "to_person_id", "label"), + UNIQUE ("from_person_id", "to_item_id", "label") ); CREATE TABLE IF NOT EXISTS "collections" ( diff --git a/processing.go b/processing.go index 01ee88f..01f6b39 100644 --- a/processing.go +++ b/processing.go @@ -109,7 +109,7 @@ func (wc *WrappedClient) processItemGraph(ig *ItemGraph, state *recursiveState) // insert relations to this connected node into DB for _, rel := range relations { - _, err = wc.tl.db.Exec(`INSERT INTO relationships + _, err = wc.tl.db.Exec(`INSERT OR IGNORE INTO relationships (from_item_id, to_item_id, directed, label) VALUES (?, ?, ?, ?)`, igRowID, connectedIGRowID, !rel.Bidirectional, rel.Label) @@ -156,7 +156,7 @@ func (wc *WrappedClient) processItemGraph(ig *ItemGraph, state *recursiveState) } // store the relation - _, err = wc.tl.db.Exec(`INSERT INTO relationships + _, err = wc.tl.db.Exec(`INSERT OR IGNORE INTO relationships (from_item_id, to_item_id, directed, label) VALUES (?, ?, ?, ?)`, fromItemRowID, toItemRowID, rr.Bidirectional, rr.Label)