From e9dbfe33e3a32c6e615562d3625fd13affe720de Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 17 Jul 2024 19:49:50 -0400 Subject: [PATCH] proper foreign key --- pkg/gordio/database/talkgroups.sql.go | 7 ++++--- sql/postgres/migrations/001_initial.up.sql | 7 ++++--- sql/postgres/queries/talkgroups.sql | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/gordio/database/talkgroups.sql.go b/pkg/gordio/database/talkgroups.sql.go index 9362bd8..4e40834 100644 --- a/pkg/gordio/database/talkgroups.sql.go +++ b/pkg/gordio/database/talkgroups.sql.go @@ -82,16 +82,17 @@ func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ( } const setTalkgroupTags = `-- name: SetTalkgroupTags :exec -INSERT INTO talkgroups_tags(talkgroup_id, tags) VALUES($1, $2) -ON CONFLICT (talkgroup_id) DO UPDATE SET tags = $2 +INSERT INTO talkgroups_tags(system_id, talkgroup_id, tags) VALUES($1, $2, $3) +ON CONFLICT (system_id, talkgroup_id) DO UPDATE SET tags = $3 ` type SetTalkgroupTagsParams struct { + SystemID int32 TalkgroupID int32 Tags []string } func (q *Queries) SetTalkgroupTags(ctx context.Context, arg SetTalkgroupTagsParams) error { - _, err := q.db.Exec(ctx, setTalkgroupTags, arg.TalkgroupID, arg.Tags) + _, err := q.db.Exec(ctx, setTalkgroupTags, arg.SystemID, arg.TalkgroupID, arg.Tags) return err } diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 7308cb5..2e8b32d 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -36,9 +36,10 @@ CREATE TABLE IF NOT EXISTS talkgroups( ); CREATE TABLE IF NOT EXISTS talkgroups_tags( - system_id INTEGER NOT NULL REFERENCES systems(id), - talkgroup_id INTEGER NOT NULL REFERENCES talkgroups(id), - tags TEXT[] NOT NULL DEFAULT '{}' + system_id INTEGER NOT NULL, + talkgroup_id INTEGER NOT NULL, + tags TEXT[] NOT NULL DEFAULT '{}', + FOREIGN KEY (system_id, talkgroup_id) REFERENCES talkgroups (system, tgid) ); CREATE UNIQUE INDEX IF NOT EXISTS talkgroups_tags_id_talkgroup_id ON talkgroups_tags(system_id, talkgroup_id); CREATE INDEX IF NOT EXISTS talkgroup_tags_id_tags ON talkgroups_tags USING GIN (tags); diff --git a/sql/postgres/queries/talkgroups.sql b/sql/postgres/queries/talkgroups.sql index 7680bff..2af1f1e 100644 --- a/sql/postgres/queries/talkgroups.sql +++ b/sql/postgres/queries/talkgroups.sql @@ -11,6 +11,6 @@ SELECT tags FROM talkgroups_tags WHERE talkgroup_id = $1; -- name: SetTalkgroupTags :exec -INSERT INTO talkgroups_tags(talkgroup_id, tags) VALUES($1, $2) -ON CONFLICT (talkgroup_id) DO UPDATE SET tags = $2; +INSERT INTO talkgroups_tags(system_id, talkgroup_id, tags) VALUES($1, $2, $3) +ON CONFLICT (system_id, talkgroup_id) DO UPDATE SET tags = $3;