sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

Generate Active Model from Two Models' Differences

Open SamuNatsu opened this issue 6 months ago • 3 comments

Motivation

The model is serialized then edited by an external editor, which would deserialize back to a new model after editing.

Do a full update is ok, while find the diffs between old and new model for on-demand updating is more clever.

Proposed Solutions

Provide a new method for derive macros like fn create_diff_active_model(old_model: &Model) -> ActiveModel is enough.

Additional Information

SamuNatsu avatar May 25 '26 08:05 SamuNatsu

@SamuNatsu can you assign this issue to me ?? or I can directly raise PR ?

mansiverma897993 avatar Jun 07 '26 07:06 mansiverma897993

@SamuNatsu can you assign this issue to me ?? or I can directly raise PR ?

I'm not the maintainer of the project, so assigning the issue to you is technically impossible :(

Raise PR with a reference #3067 is enough

SamuNatsu avatar Jun 07 '26 09:06 SamuNatsu

I just encountered a case where this would be useful at my job! This is a great idea.

Until it's implemented by a SeaORM derive macro, I suggest using "Destructure as a Reminder" idiom to make sure to cover all fields in your manual implementation. Mine looks like this:

fn merge_models_and_mark_changes(old: foo::Model, new: foo::Model) -> foo::ActiveModel {
  let mut result = old.into_active_model();
  // Intentionally unpack so that we get warnings for unused fields
  // and a compiler error here when a new fields is added.
  let foo::ActiveModel { bar, baz } = &mut result;
  bar.set_ne(new.bar);
  baz.set_ne(new.baz);
  result
}

Also note that a reference (old_model: &Model) in your proposed signature is a tradeoff. It requires you to clone out the value when it's Unchanged. Which is actually more work when you no longer need the old model and are fine with consuming it.

Expurple avatar Jun 08 '26 11:06 Expurple