Skip to content

feat: skip batch insert when unique index exists#1440

Merged
qqxhb merged 4 commits intomasterfrom
fix/issue-1115-unique-aware
Feb 27, 2026
Merged

feat: skip batch insert when unique index exists#1440
qqxhb merged 4 commits intomasterfrom
fix/issue-1115-unique-aware

Conversation

@qqxhb
Copy link
Member

@qqxhb qqxhb commented Feb 27, 2026

Fixes #1115

When generating query unit tests, the CRUD template inserts multiple empty records. For models with unique indexes, batch inserts can fail because duplicates share the same zero values.

This PR includes two changes:

  • Skip CreateInBatches when any field contains a uniqueIndex gorm tag (tag-only detection)
  • Add custom unit test template support via gen.Config.UnitTestTemplate and gentool (yaml/flag)

Usage:

  • YAML: database.unitTestTemplate: "/path/to/template.tmpl"
  • CLI: -unitTestTemplate /path/to/template.tmpl

Tests:

  • go test ./...

@propel-code-bot
Copy link

propel-code-bot bot commented Feb 27, 2026

It also updates the gentool documentation and sample configuration to reflect the new unit test template option.

Affected Areas

generator.go
config.go
internal/generate/query.go
internal/template/method.go
tools/gentool/gentool.go
tools/gentool/README.md
tools/gentool/README.ZH_CN.md
tools/gentool/gen.yml

This summary was automatically generated by @propel-code-bot

if f == nil {
continue
}
if len(f.GORMTag[field.TagKeyGormUniqueIndex]) > 0 {

Choose a reason for hiding this comment

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

Important

[Logic] The PR description says to skip CreateInBatches when any field has a gorm tag, but HasUniqueIndex() only checks uniqueIndex. Models using other uniqueness-related tags (e.g., unique, primaryKey) will still run the batch insert and can still fail with duplicate zero values. If the intent is truly “any gorm tag,” consider checking the tag map directly or expanding the tag set you check.

Example (if you want any gorm tag to disable batching):

if len(f.GORMTag) > 0 {
    return true
}

Or include other uniqueness tags as needed.

Context for Agents
The PR description says to skip `CreateInBatches` when **any** field has a gorm tag, but `HasUniqueIndex()` only checks `uniqueIndex`. Models using other uniqueness-related tags (e.g., `unique`, `primaryKey`) will still run the batch insert and can still fail with duplicate zero values. If the intent is truly “any gorm tag,” consider checking the tag map directly or expanding the tag set you check.

Example (if you want any gorm tag to disable batching):

```go
if len(f.GORMTag) > 0 {
    return true
}
```

Or include other uniqueness tags as needed.

File: internal/generate/query.go
Line: 145

if f == nil {
continue
}
if len(f.GORMTag[field.TagKeyGormUniqueIndex]) > 0 {

Choose a reason for hiding this comment

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

Important

[Logic] HasUniqueIndex only checks model.Field.GORMTag, but for struct-based generation (ConvertStructsparseStruct) GORMTag is never populated. That means uniqueIndex tags on model structs won’t be detected and CreateInBatches will still run, reintroducing the duplicate failure. Consider populating GORMTag from schema.Field.TagSettings in parseStruct, or extend HasUniqueIndex to also check the struct tag settings so struct-derived models are covered.

Context for Agents
HasUniqueIndex only checks `model.Field.GORMTag`, but for struct-based generation (`ConvertStructs` → `parseStruct`) `GORMTag` is never populated. That means `uniqueIndex` tags on model structs won’t be detected and `CreateInBatches` will still run, reintroducing the duplicate failure. Consider populating `GORMTag` from `schema.Field.TagSettings` in `parseStruct`, or extend `HasUniqueIndex` to also check the struct tag settings so struct-derived models are covered.

File: internal/generate/query.go
Line: 145

@qqxhb qqxhb merged commit 0cb2450 into master Feb 27, 2026
15 of 16 checks passed
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.

GEN Generate unique aware unit tests.

1 participant