$npx -y skills add saifoelloh/golang-best-practices-skill --skill gorm-query-patternsGORM ORM query pattern review for PostgreSQL. Use when reviewing or writing Go database code using GORM — covers N+1, Preload vs Joins, transactions, error checking, Save vs Updates, and context propagation. Trigger on any GORM method usage.
| 1 | # GORM Query Patterns |
| 2 | |
| 3 | Expert-level review of GORM ORM usage in Go services backed by PostgreSQL. Covers correctness, safety, and idiomatic patterns. |
| 4 | |
| 5 | ## When to Apply |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Writing or reviewing repository layer code using GORM |
| 9 | - Debugging unexpected query behavior (wrong results, wrong ORDER, missing data) |
| 10 | - Reviewing association loading (Preload, Joins) |
| 11 | - Auditing multi-step write operations for transaction safety |
| 12 | |
| 13 | ## Rule Categories |
| 14 | |
| 15 | | Priority | Count | Focus | |
| 16 | |---|---|---| |
| 17 | | Critical | 3 | SQL injection, silent errors, partial writes | |
| 18 | | High | 5 | N+1, First vs Find, Save vs Updates, context, column selection | |
| 19 | | Medium | 3 | Scopes, pagination, association upserts | |
| 20 | |
| 21 | ## Rules Covered (11 total) |
| 22 | |
| 23 | ### Critical Issues (3) |
| 24 | - `critical-sql-injection` — Never concatenate user input into SQL strings |
| 25 | - `critical-error-check` — Always check `.Error` after every GORM operation |
| 26 | - `critical-transaction-wrap` — Wrap multi-step operations in `db.Transaction()` |
| 27 | |
| 28 | ### High-Impact Patterns (5) |
| 29 | - `high-preload-vs-joins` — Preload for loading, Joins for filtering |
| 30 | - `high-first-vs-find` — First for single record, Find for lists |
| 31 | - `high-save-vs-updates` — Use Updates with map for partial updates, not Save |
| 32 | - `high-with-context` — Always pass context via `db.WithContext(ctx)` |
| 33 | - `high-select-columns` — Select only required columns, never `SELECT *` |
| 34 | |
| 35 | ### Medium Improvements (3) |
| 36 | - `medium-scopes` — Extract reusable WHERE logic into GORM scopes |
| 37 | - `medium-pagination-order` — Always include ORDER BY in paginated queries |
| 38 | - `medium-omit-associations` — Use Omit to prevent unintended association upserts |
| 39 | |
| 40 | ## Trigger Phrases |
| 41 | |
| 42 | - "Review this GORM query" |
| 43 | - "Check repository layer" |
| 44 | - "db.Find / db.First / db.Create / db.Where" |
| 45 | - "Preload / Joins" |
| 46 | - "GORM transaction" |
| 47 | - "Is this GORM code correct?" |
| 48 | |
| 49 | ## Related Skills |
| 50 | |
| 51 | - [postgresql-syntax](../postgresql-syntax/SKILL.md) — For raw SQL correctness and Postgres-specific syntax |
| 52 | - [query-performance](../query-performance/SKILL.md) — For index usage and N+1 detection |
| 53 | - [error-handling](../error-handling/SKILL.md) — For GORM and PostgreSQL error handling patterns |
| 54 | - **golang-best-practices-skill** `database-repository` — Companion Go skill covering identifier quoting and select redundancy |
| 55 | |
| 56 | ## Output Format |
| 57 | |
| 58 | ``` |
| 59 | ## Critical Issues Found: X |
| 60 | |
| 61 | ### [Rule ID] (File: repository.go, approx. line Y) |
| 62 | **Issue**: Brief description |
| 63 | **Fix**: Suggested correction |
| 64 | **Example**: |
| 65 | ```go |
| 66 | // Corrected code here |
| 67 | ``` |
| 68 | |
| 69 | ## High-Impact Patterns: X |
| 70 | ## Medium Improvements: X |
| 71 | ``` |