rel icon indicating copy to clipboard operation
rel copied to clipboard

Allow to manually specify field names that need to be updated

Open lafriks opened this issue 2 years ago • 4 comments

In some cases you need to specifically save to database only some fields from struct and do not update any others. So it would be nice to specify something like:

db.Update(ctx, &t, rel.Fields("first_name", "last_name", "date_modifed"))

Also would be nice to specify fields in struct comments that this field should never be included in update (pk already is excluded but there are some fields that also can not be updated (like for partitioned tables etc). created_at should probably be excluded by default for example

Currently in postgres I'm having error ERROR: modifying the partition value of rows is not allowed (SQLSTATE 0A000) because of that and only option is to make update manually for this case

lafriks avatar Aug 18 '23 13:08 lafriks

probably we also need to provide exclude function?

Fs02 avatar Aug 18 '23 15:08 Fs02

Probably both would be nice to have

lafriks avatar Aug 18 '23 21:08 lafriks

I tried to look into this but I lack a bit of knowledge how changesets are built and where to add these options

lafriks avatar Aug 18 '23 21:08 lafriks

maybe as pointer:

  • define a mutator, example: https://github.com/go-rel/rel/blob/master/on_conflict.go#L13
  • register the mutator here: https://github.com/go-rel/rel/blob/master/mutation.go#L31
  • use mutator here: https://github.com/go-rel/rel/blob/master/structset.go#L32

there's also map type: https://github.com/go-rel/rel/blob/master/map.go#L22

Fs02 avatar Aug 19 '23 00:08 Fs02