Register all persistent objects from a flush. This is used both for pending objects moving to the persistent state as well as already persistent objects.
(self, states: Set[InstanceState[Any]])
| 3443 | ) |
| 3444 | |
| 3445 | def _register_persistent(self, states: Set[InstanceState[Any]]) -> None: |
| 3446 | """Register all persistent objects from a flush. |
| 3447 | |
| 3448 | This is used both for pending objects moving to the persistent |
| 3449 | state as well as already persistent objects. |
| 3450 | |
| 3451 | """ |
| 3452 | |
| 3453 | pending_to_persistent = self.dispatch.pending_to_persistent or None |
| 3454 | for state in states: |
| 3455 | mapper = _state_mapper(state) |
| 3456 | |
| 3457 | # prevent against last minute dereferences of the object |
| 3458 | obj = state.obj() |
| 3459 | if obj is not None: |
| 3460 | instance_key = mapper._identity_key_from_state(state) |
| 3461 | |
| 3462 | if ( |
| 3463 | _none_set.intersection(instance_key[1]) |
| 3464 | and not mapper.allow_partial_pks |
| 3465 | or _none_set.issuperset(instance_key[1]) |
| 3466 | ): |
| 3467 | raise exc.FlushError( |
| 3468 | "Instance %s has a NULL identity key. If this is an " |
| 3469 | "auto-generated value, check that the database table " |
| 3470 | "allows generation of new primary key values, and " |
| 3471 | "that the mapped Column object is configured to " |
| 3472 | "expect these generated values. Ensure also that " |
| 3473 | "this flush() is not occurring at an inappropriate " |
| 3474 | "time, such as within a load() event." |
| 3475 | % state_str(state) |
| 3476 | ) |
| 3477 | |
| 3478 | if state.key is None: |
| 3479 | state.key = instance_key |
| 3480 | elif state.key != instance_key: |
| 3481 | # primary key switch. use safe_discard() in case another |
| 3482 | # state has already replaced this one in the identity |
| 3483 | # map (see test/orm/test_naturalpks.py ReversePKsTest) |
| 3484 | self.identity_map.safe_discard(state) |
| 3485 | trans = self._transaction |
| 3486 | assert trans is not None |
| 3487 | if state in trans._key_switches: |
| 3488 | orig_key = trans._key_switches[state][0] |
| 3489 | else: |
| 3490 | orig_key = state.key |
| 3491 | trans._key_switches[state] = ( |
| 3492 | orig_key, |
| 3493 | instance_key, |
| 3494 | ) |
| 3495 | state.key = instance_key |
| 3496 | |
| 3497 | # there can be an existing state in the identity map |
| 3498 | # that is replaced when the primary keys of two instances |
| 3499 | # are swapped; see test/orm/test_naturalpks.py -> test_reverse |
| 3500 | old = self.identity_map.replace(state) |
| 3501 | if ( |
| 3502 | old is not None |
no test coverage detected