make history meta handle nullable and default better

* can now handle nullable foreign keys - would previously raise
  AttributeError
* can now handle default values - we would previously not insert
  default values that hadn't been generated yet, which if the
  field is not nullable will result in IntegrityErrors. We were
  deliberately removing 'default' attributes from columns. Only
  remove them if nullable is set to true now.
This commit is contained in:
Leo Hemsted
2016-08-04 15:57:41 +01:00
parent a6cd490942
commit 8e46cd44fd
2 changed files with 20 additions and 7 deletions

View File

@@ -53,7 +53,12 @@ def _history_mapper(local_mapper):
col = col.copy()
orig.info['history_copy'] = col
col.unique = False
col.default = col.server_default = None
# if the column is nullable, we could end up overwriting an on-purpose null value with a default.
# if it's not nullable, however, the default may be relied upon to correctly set values within the database,
# so we should preserve it
if col.nullable:
col.default = col.server_default = None
return col
properties = util.OrderedDict()
@@ -201,7 +206,9 @@ def create_history(obj):
elif isinstance(prop, RelationshipProperty):
if hasattr(history, prop.key+'_id'):
data[prop.key+'_id'] = getattr(obj, prop.key).id
foreign_obj = getattr(obj, prop.key)
# if it's a nullable relationship, foreign_obj will be None, and we actually want to record that
data[prop.key+'_id'] = getattr(foreign_obj, 'id', None)
if not obj.version:
obj.version = 1