Keep relationships unique

Bidirectional is not part of the unique constraints because I don't
think it's relevant when checking for uniqueness.
This commit is contained in:
Matthew Holt
2019-02-20 14:13:09 -07:00
parent 42fc84848d
commit 4bbfd951dc
2 changed files with 8 additions and 4 deletions

8
db.go
View File

@@ -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" (

View File

@@ -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)