Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

replace just the changed fields in the model #187

legrostdg opened this Issue Nov 27, 2013 · 4 comments


None yet
3 participants

As discussed in the mailing list, it would be cool to have a kind of "diffToUpdates" in persistent which would only update fields (or return a list of Update's) which differ between two models:

For example:

data Person = Person { name :: String, age :: Int }

oldModel = Person "Michael" 26

newModel = Person "Michael" 27

"diffToUpdates oldModel newModel" would give "[PersonAge =. 27]"


gregwebs commented Aug 29, 2014

Note that updateGet is another way of getting at this: apply updates to the db and get back the newModel.


gregwebs commented Sep 23, 2014

I think I would call such a function overwriteUpdates. Although updateGet is still preferable, this is needed to merge 2 existing records


gregwebs commented Sep 23, 2014

overwriteUpdates :: (PersistEntity record, Eq record) => record -> record -> [Update record]
overwriteUpdates record1 record2 = catMaybes $ zipWith overwrite fields1 fields2
    overwrite up1@(field1 =. pf1) up2@(field2 =. pf2) = if toPersistValue pf1 == toPersistValue pf2 then Nothing else Just up2
    fields1 = toUpdates record1
    fields2 = toUpdates record2
    entity = entityDef $ Just record1

The only problem is that toUpdates does not exist! As far as I can tell it needs to be generated in template-haskell for each record, because EntityField is essentially a GADT that we can't just automatically derive Enum and Bounded for.

This feature can be added without breaking backwards compatibility.


snoyberg commented Jul 19, 2015

Closing out old issues, please reopen if still relevant

@snoyberg snoyberg closed this Jul 19, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment