diff --git a/bin/templates/pkg/db/crud/crud_table_update_func.go_ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ index 096bd13..c660965 100644 --- a/bin/templates/pkg/db/crud/crud_table_update_func.go_ +++ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ @@ -30,7 +30,14 @@ func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.Lawsui db.WithContext(ctx) // - tx := db.Model(&m).Update("ColumnName", m.ColumnName) + var tx *gorm.DB + Value := m.ColumnName + if 0==1 && Value == 0 { + tx = db.Model(&m).Update("ColumnName", gorm.Expr("NULL")) + } else { + tx = db.Model(&m).Update("ColumnName", Value) + } + err = tx.Error if err != nil { err = fmt.Errorf("Read() id: %v, error: %v", m.ID, err) diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index d44d20a..4dd09d2 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -1218,3 +1218,17 @@ func FindTextConvertProtobufTypeToGolangType(Table1 *types.Table, Column1 *types return Otvet } + +// FindTextEqualEmpty - находит текст сравнение с пустым значением +func FindTextEqualEmpty(Column1 *types.Column, VariableName string) string { + Otvet := "" + + DefaultValue := FindTextDefaultValue(Column1.TypeGo) + Otvet = VariableName + "." + Column1.NameGo + " == " + DefaultValue + + if DefaultValue == "time.Time{}" { + Otvet = VariableName + "." + Column1.NameGo + ".IsZero() == true" + } + + return Otvet +} diff --git a/internal/create_files/db_crud_tables/db_crud_tables.go b/internal/create_files/db_crud_tables/db_crud_tables.go index 77736f4..4d8e45b 100644 --- a/internal/create_files/db_crud_tables/db_crud_tables.go +++ b/internal/create_files/db_crud_tables/db_crud_tables.go @@ -487,6 +487,7 @@ func CreateFilesUpdateEveryColumn(Table1 *types.Table) error { "context" "fmt" "time" + "gorm.io/gorm" "github.com/ManyakRus/starter/contextmain" "github.com/ManyakRus/starter/micro" "github.com/ManyakRus/starter/postgres_gorm" @@ -586,6 +587,13 @@ func FindTextUpdateEveryColumn1(TextCrudUpdateFunc string, Table1 *types.Table, //Otvet = strings.ReplaceAll(Otvet, "ColumnName", ColumnName) //Otvet = strings.ReplaceAll(Otvet, "m.ID", "m."+ColumnName) + //внешние ключи заменяем 0 на null + if Column1.IsNullable == true && (Column1.TableKey != "" || Column1.TypeGo == "time.Time{}") { + Otvet = strings.ReplaceAll(Otvet, "0==1", "1==1") + TextEqualEmpty := create_files.FindTextEqualEmpty(Column1, "Value") + Otvet = strings.ReplaceAll(Otvet, "Value == 0", TextEqualEmpty) + } + return Otvet }