12
from sqlalchemy.orm import exc, util as mapperutil, attributes
12
from . import exc, util as orm_util, attributes
14
15
def populate(source, source_mapper, dest, dest_mapper,
15
16
synchronize_pairs, uowcommit, flag_cascaded_pks):
43
44
uowcommit.attributes[("pk_cascaded", dest, r)] = True
45
47
def clear(dest, dest_mapper, synchronize_pairs):
46
48
for l, r in synchronize_pairs:
48
50
raise AssertionError(
49
"Dependency rule tried to blank-out primary key "
50
"column '%s' on instance '%s'" %
51
(r, mapperutil.state_str(dest))
51
"Dependency rule tried to blank-out primary key "
52
"column '%s' on instance '%s'" %
53
(r, orm_util.state_str(dest))
54
56
dest_mapper._set_state_attr_by_column(dest, dest.dict, r, None)
55
57
except exc.UnmappedColumnError:
56
58
_raise_col_to_prop(True, None, l, dest_mapper, r)
58
61
def update(source, source_mapper, dest, old_prefix, synchronize_pairs):
59
62
for l, r in synchronize_pairs:
61
oldvalue = source_mapper._get_committed_attr_by_column(source.obj(), l)
62
value = source_mapper._get_state_attr_by_column(source, source.dict, l)
64
oldvalue = source_mapper._get_committed_attr_by_column(
66
value = source_mapper._get_state_attr_by_column(
67
source, source.dict, l)
63
68
except exc.UnmappedColumnError:
64
69
_raise_col_to_prop(False, source_mapper, l, None, r)
65
70
dest[r.key] = value
66
71
dest[old_prefix + r.key] = oldvalue
68
74
def populate_dict(source, source_mapper, dict_, synchronize_pairs):
69
75
for l, r in synchronize_pairs:
71
value = source_mapper._get_state_attr_by_column(source, source.dict, l)
77
value = source_mapper._get_state_attr_by_column(
78
source, source.dict, l)
72
79
except exc.UnmappedColumnError:
73
80
_raise_col_to_prop(False, source_mapper, l, None, r)
75
82
dict_[r.key] = value
77
85
def source_modified(uowcommit, source, source_mapper, synchronize_pairs):
78
86
"""return true if the source object has changes from an old to a
79
87
new value on the given synchronize pairs
86
94
_raise_col_to_prop(False, source_mapper, l, None, r)
87
95
history = uowcommit.get_attribute_history(source, prop.key,
88
96
attributes.PASSIVE_NO_INITIALIZE)
89
return bool(history.deleted)
97
if bool(history.deleted):
93
def _raise_col_to_prop(isdest, source_mapper, source_column, dest_mapper, dest_column):
103
def _raise_col_to_prop(isdest, source_mapper, source_column,
104
dest_mapper, dest_column):
95
raise exc.UnmappedColumnError(
96
"Can't execute sync rule for destination column '%s'; "
97
"mapper '%s' does not map this column. Try using an explicit"
98
" `foreign_keys` collection which does not include this column "
99
"(or use a viewonly=True relation)." % (dest_column, dest_mapper)
106
raise exc.UnmappedColumnError("Can't execute sync rule for "
107
"destination column '%s'; mapper '%s' does not map "
108
"this column. Try using an explicit `foreign_keys` "
109
"collection which does not include this column (or use "
110
"a viewonly=True relation)." % (dest_column,
102
raise exc.UnmappedColumnError(
103
"Can't execute sync rule for source column '%s'; mapper '%s' "
104
"does not map this column. Try using an explicit `foreign_keys`"
105
" collection which does not include destination column '%s' (or "
106
"use a viewonly=True relation)." %
107
(source_column, source_mapper, dest_column)
113
raise exc.UnmappedColumnError("Can't execute sync rule for "
114
"source column '%s'; mapper '%s' does not map this "
115
"column. Try using an explicit `foreign_keys` "
116
"collection which does not include destination column "
117
"'%s' (or use a viewonly=True relation)."
118
% (source_column, source_mapper, dest_column))