For many-to-one relationships with no one-to-many backref, searches for parents through the unit of work when a primary key has changed and updates them. Theoretically, this approach could be expanded to support transparent deletion of objects referenced via many-to-one as well, alt
| 864 | |
| 865 | |
| 866 | class DetectKeySwitch(DependencyProcessor): |
| 867 | """For many-to-one relationships with no one-to-many backref, |
| 868 | searches for parents through the unit of work when a primary |
| 869 | key has changed and updates them. |
| 870 | |
| 871 | Theoretically, this approach could be expanded to support transparent |
| 872 | deletion of objects referenced via many-to-one as well, although |
| 873 | the current attribute system doesn't do enough bookkeeping for this |
| 874 | to be efficient. |
| 875 | |
| 876 | """ |
| 877 | |
| 878 | def per_property_preprocessors(self, uow): |
| 879 | if self.prop._reverse_property: |
| 880 | if self.passive_updates: |
| 881 | return |
| 882 | else: |
| 883 | if False in ( |
| 884 | prop.passive_updates |
| 885 | for prop in self.prop._reverse_property |
| 886 | ): |
| 887 | return |
| 888 | |
| 889 | uow.register_preprocessor(self, False) |
| 890 | |
| 891 | def per_property_flush_actions(self, uow): |
| 892 | parent_saves = unitofwork.SaveUpdateAll(uow, self.parent.base_mapper) |
| 893 | after_save = unitofwork.ProcessAll(uow, self, False, False) |
| 894 | uow.dependencies.update([(parent_saves, after_save)]) |
| 895 | |
| 896 | def per_state_flush_actions(self, uow, states, isdelete): |
| 897 | pass |
| 898 | |
| 899 | def presort_deletes(self, uowcommit, states): |
| 900 | pass |
| 901 | |
| 902 | def presort_saves(self, uow, states): |
| 903 | if not self.passive_updates: |
| 904 | # for non-passive updates, register in the preprocess stage |
| 905 | # so that mapper save_obj() gets a hold of changes |
| 906 | self._process_key_switches(states, uow) |
| 907 | |
| 908 | def prop_has_changes(self, uow, states, isdelete): |
| 909 | if not isdelete and self.passive_updates: |
| 910 | d = self._key_switchers(uow, states) |
| 911 | return bool(d) |
| 912 | |
| 913 | return False |
| 914 | |
| 915 | def process_deletes(self, uowcommit, states): |
| 916 | assert False |
| 917 | |
| 918 | def process_saves(self, uowcommit, states): |
| 919 | # for passive updates, register objects in the process stage |
| 920 | # so that we avoid ManyToOneDP's registering the object without |
| 921 | # the listonly flag in its own preprocess stage (results in UPDATE) |
| 922 | # statements being emitted |
| 923 | assert self.passive_updates |