diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 4b85164d5f..4f3cf4380a 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -305,9 +305,9 @@ class ModelSQL(ModelStorage): history = cls.__table_history__() columns = [] hcolumns = [] - for fname, field in sorted(cls._fields.iteritems()): - if hasattr(field, 'set'): - continue + fnames = sorted(n for n, f in cls._fields.iteritems() + if not hasattr(f, 'set')) + for fname in fnames: columns.append(Column(table, fname)) if fname == 'write_uid': hcolumns.append(Literal(transaction.user)) @@ -316,6 +316,10 @@ class ModelSQL(ModelStorage): else: hcolumns.append(Column(history, fname)) + def is_deleted(values): + return all(not v for n, v in zip(fnames, values) + if n not in ['id', 'write_uid', 'write_date']) + to_delete = [] to_update = [] for id_ in ids: @@ -325,7 +329,7 @@ class ModelSQL(ModelStorage): cursor.execute(*history.select(*hcolumns, where=hwhere, order_by=horder, limit=1)) values = cursor.fetchone() - if not values: + if not values or is_deleted(values): to_delete.append(id_) else: to_update.append(id_) diff --git a/trytond/tests/test_history.py b/trytond/tests/test_history.py index 25a9adfb00..74c9176ec1 100644 --- a/trytond/tests/test_history.py +++ b/trytond/tests/test_history.py @@ -184,6 +184,15 @@ class HistoryTestCase(unittest.TestCase): History.restore_history([history_id], datetime.datetime.min) self.assertRaises(UserError, History.read, [history_id]) + with Transaction().start(DB_NAME, USER, context=CONTEXT): + History.delete([History(history_id)]) + + transaction.cursor.commit() + + with Transaction().start(DB_NAME, USER, context=CONTEXT): + History.restore_history([history_id], datetime.datetime.max) + self.assertRaises(UserError, History.read, [history_id]) + @unittest.skipIf(backend.name() in ('sqlite', 'mysql'), 'now() is not the start of the transaction') def test0045restore_history_same_timestamp(self):