feat: skip batch insert when unique index exists#1440
Conversation
|
It also updates the gentool documentation and sample configuration to reflect the new unit test template option. Affected Areas• This summary was automatically generated by @propel-code-bot |
| if f == nil { | ||
| continue | ||
| } | ||
| if len(f.GORMTag[field.TagKeyGormUniqueIndex]) > 0 { |
There was a problem hiding this comment.
[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 { |
There was a problem hiding this comment.
[Logic] 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.
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
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:
Usage:
Tests: