Skip to content

Fix tags table missing PRIMARY KEY causing duplicate tag display#504

Merged
compscidr merged 3 commits intomainfrom
fix/tags-table-missing-primary-key
Mar 16, 2026
Merged

Fix tags table missing PRIMARY KEY causing duplicate tag display#504
compscidr merged 3 commits intomainfrom
fix/tags-table-missing-primary-key

Conversation

@compscidr
Copy link
Copy Markdown
Collaborator

Summary

Root cause: The tags table on staging was created by an older GORM migration without a PRIMARY KEY:

-- Actual (broken): no constraint, allows duplicates
CREATE TABLE "tags" ("name" varchar(255))

-- Expected: unique names enforced
CREATE TABLE "tags" ("name" varchar(255) PRIMARY KEY)

This allowed duplicate rows (e.g. 14 rows for "android"). GORM's Preload("Tags") JOINs through post_tagstags, returning all matching tag rows per join entry, so posts displayed duplicated tags.

Staging data: 610 total tag rows, only 545 unique — 65 duplicates.

Fixes

  • Add fixTagsTable migration that detects the missing PK, deduplicates with INSERT OR IGNORE INTO tags_new SELECT DISTINCT name FROM tags, and rebuilds with a proper PRIMARY KEY
  • Reload UpdatePost response into a fresh Post struct to prevent GORM from appending preloaded tags to the existing slice

Verified against staging DB copy

  • Before: post 113 returned 20 tags (3 unique × duplicates)
  • After: post 113 returns exactly 3 tags

Test plan

  • All existing tests pass
  • Verified fix against staging DB copy
  • Deploy and confirm tags display correctly on staging

🤖 Generated with Claude Code

The tags table was created by an older GORM version without a PRIMARY
KEY constraint on the name column. This allowed duplicate tag rows
(e.g. 14 rows for "android"), and GORM's Preload("Tags") JOIN returns
all matching rows, causing tags to appear duplicated in post views.

- Add fixTagsTable migration that detects the missing PK, deduplicates
  rows with INSERT OR IGNORE INTO ... SELECT DISTINCT, and rebuilds
  the table with a proper PRIMARY KEY
- Reload UpdatePost response into a fresh struct to prevent GORM from
  appending preloaded tags to the existing slice

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 16, 2026 20:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves data integrity and API correctness around tags and post updates by adding a SQLite migration repair for legacy tags schemas and by preventing GORM from accumulating duplicated slice associations in the admin update response.

Changes:

  • Add a SQLite-only migration step to rebuild tags with name as a PRIMARY KEY and deduplicate existing rows when the table lacks a PK.
  • Update Admin.UpdatePost to reload the updated post into a fresh struct before returning JSON (avoids GORM appending to existing slices).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
tools/migrate.go Adds fixTagsTable migration helper and invokes it during Migrate() to repair legacy tags schemas missing a primary key.
admin/admin.go Reloads the updated post into a new struct before computing backlinks and returning the JSON response.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

compscidr and others added 2 commits March 16, 2026 13:18
- Distinguish sql.ErrNoRows from real errors in fixTagsTable Scan
- Check error on post reload query in UpdatePost, return 500 on failure
- Add TestMigrationFixesTagsWithoutPrimaryKey: creates legacy tags
  table without PK, inserts duplicates, runs Migrate, verifies
  deduplication, PK presence, and duplicate rejection

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@compscidr compscidr merged commit 9fba59c into main Mar 16, 2026
1 check passed
@compscidr compscidr deleted the fix/tags-table-missing-primary-key branch March 16, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants