返回提示词目录第 1152 页

编写数据库模型与查询代码

按项目约定生成数据库模型、版本化迁移与读写代码,并覆盖零值更新等关键测试。

完整提示词

<context>
GORM is convenient and full of silent behaviour. Errors are returned on the result (`db.First(&u).Error`), not as a second value, so unchecked calls swallow failures; `First`, `Take` and `Last` return `gorm.ErrRecordNotFound`, but `Find` returns no error on zero rows. Updating with a struct (`Updates(User{Active: false})`) skips zero values, so setting a field to false, 0 or "" does nothing; a `map[string]any` or `Select` with named fields fixes it. `Save` writes every field, overwriting concurrent changes. A `DeletedAt gorm.DeletedAt` field turns on soft delete and adds `deleted_at IS NULL` to every query, which also breaks unique indexes unless they include `deleted_at` or use a partial index. `AutoMigrate` adds tables and columns but never drops or renames and cannot express data changes, so production schemas drift; a versioned migration tool (goose, golang-migrate, Atlas) is the safe path. Associations are not loaded unless `Preload` or `Joins` is used, and looping over parents to load children is N+1. Saving a struct with associations also upserts them by default (`Omit(clause.Associations)` stops it). `Where("name = " + input)` is injection, while `Where("name = ?", input)` is not. Pass `WithContext(ctx)` so queries respect timeouts and cancellation.
</context>

<task>
Data model and queries: {{data_model}}
{{#setup}}Setup: {{setup}}{{/setup}}

1. Read the existing models, how the `*gorm.DB` is created (logger, `PrepareStmt`, naming strategy), migration approach, and the repositories or services that query the affected tables. Follow the project's conventions.
2. Write the models: explicit primary key, column types and sizes in tags, `not null`, defaults, indexes and unique indexes (soft-delete-aware where `DeletedAt` is used), money as integer minor units or a decimal type, and associations with explicit `foreignKey` and `constraint:OnDelete` tags.
3. Write the migration with the project's migration tool as SQL (or generate a diff with it), review it, and keep `AutoMigrate` to tests or local development only.
4. Write the queries: `WithContext(ctx)` on every call, checked `.Error`, `ErrRecordNotFound` mapped to the domain's not-found error, `Preload` or `Joins` for associations the caller reads, maps or `Select` for updates that set zero values, and transactions with `db.Transaction(func(tx *gorm.DB) error {...})`.
5. Write tests against a real database or the project's test setup, covering not-found, an update to a zero value, soft-deleted rows, and the query count on the main list path. Run `go vet` and the tests and report the results.
</task>

<constraints>
- Do not rely on `AutoMigrate` for shared or production databases, and do not run migrations against them yourself.
- Never build query strings from input; use `?` placeholders or named arguments.
- Do not ignore `.Error` on any call.
- Do not invent fields beyond the stated model; list missing information as questions.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Models
The model structs in fenced blocks with paths.
## Design notes
Bullets on tags, indexes, soft delete, associations and delete behaviour.
## Migration
The versioned migration and how it runs.
## Queries
The repository code for the main reads and writes.
## Check
Commands run with real results.
## Open questions
Numbered, or "None".
</output_format>

可替换内容

  • {{data_model}}
  • {{#setup}}
  • {{setup}}
  • {{/setup}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}
  • [string]

分类与标签

编程开发、数据库建模、数据库迁移、关联查询、零值更新

前往 AI 图片创作