From 9eb9d71175ce71d195be9b8a835301d280a54bb0 Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Wed, 15 Nov 2023 15:43:01 +0300 Subject: [PATCH] fix sql method generation for structs with pk fields only --- tpl/macros_struct.twig | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 4f98285..5b928c4 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -86,7 +86,11 @@ type {{ json_fields_type }} struct{ {% macro table_save(ctx) %} func Save{{ ctx.s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ ctx.s.name }}) error { + {% if ctx.insert_update_expr|length > 0 %} query := "INSERT INTO `{{ ctx.table_name }}` ({{ ctx.insert_column_expr }}) VALUES ({{ ctx.insert_values_expr }}) ON DUPLICATE KEY UPDATE {{ ctx.insert_update_expr }}" + {% else %} + query := "INSERT INTO `{{ ctx.table_name }}` ({{ ctx.insert_column_expr }}) VALUES ({{ ctx.insert_values_expr }})" + {% endif %} _, saveErr := dbe.ExecContext( ctx, @@ -121,7 +125,9 @@ func Save{{ ctx.s.name }}Collection(ctx context.Context, dbe metadb.Execer, recs {%~ endfor %} ) } + {% if ctx.insert_update_expr|length > 0 %} builder.WriteString(" ON DUPLICATE KEY UPDATE {{ ctx.insert_update_expr }}") + {% endif %} _, saveErr := dbe.ExecContext(ctx, builder.String(), values...) return errors.WithStack(saveErr)