Support typed primary key helpers in gorm and bun writers

This commit is contained in:
2026-05-05 10:32:33 +02:00
parent e828d48798
commit 4303dcf59b
8 changed files with 328 additions and 39 deletions

View File

@@ -99,8 +99,8 @@ func (w *Writer) writeSingleFile(db *models.Database) error {
}
}
// Add fmt import if GetIDStr is enabled
if w.config.GenerateGetIDStr {
// Add fmt import when generated helper methods need string formatting.
if w.needsFmtImport(templateData.Models) {
templateData.AddImport("\"fmt\"")
}
@@ -189,8 +189,8 @@ func (w *Writer) writeMultiFile(db *models.Database) error {
}
}
// Add fmt import if GetIDStr is enabled
if w.config.GenerateGetIDStr {
// Add fmt import when generated helper methods need string formatting.
if w.needsFmtImport(templateData.Models) {
templateData.AddImport("\"fmt\"")
}
@@ -295,6 +295,26 @@ func (w *Writer) addRelationshipFields(modelData *ModelData, table *models.Table
}
}
func (w *Writer) needsFmtImport(models []*ModelData) bool {
if w.config.GenerateGetIDStr {
for _, model := range models {
if model.PrimaryKeyField != "" && !model.PrimaryKeyIsSQL && !model.PrimaryKeyIsStr {
return true
}
}
}
if w.config.GenerateUpdateID {
for _, model := range models {
if model.PrimaryKeyField != "" && model.PrimaryKeyIsSQL && !model.PrimaryKeyIsStr {
return true
}
}
}
return false
}
// findTable finds a table by schema and name in the database
func (w *Writer) findTable(schemaName, tableName string, db *models.Database) *models.Table {
for _, schema := range db.Schemas {