Make an initial pass across a set of states for INSERT or UPDATE. This includes splitting out into distinct lists for each, calling before_insert/before_update, obtaining key information for each state including its dictionary, mapper, the connection to use for the execution per
(base_mapper, states, uowtransaction)
| 208 | |
| 209 | |
| 210 | def _organize_states_for_save(base_mapper, states, uowtransaction): |
| 211 | """Make an initial pass across a set of states for INSERT or |
| 212 | UPDATE. |
| 213 | |
| 214 | This includes splitting out into distinct lists for |
| 215 | each, calling before_insert/before_update, obtaining |
| 216 | key information for each state including its dictionary, |
| 217 | mapper, the connection to use for the execution per state, |
| 218 | and the identity flag. |
| 219 | |
| 220 | """ |
| 221 | |
| 222 | for state, dict_, mapper, connection in _connections_for_states( |
| 223 | base_mapper, uowtransaction, states |
| 224 | ): |
| 225 | has_identity = bool(state.key) |
| 226 | |
| 227 | instance_key = state.key or mapper._identity_key_from_state(state) |
| 228 | |
| 229 | row_switch = update_version_id = None |
| 230 | |
| 231 | # call before_XXX extensions |
| 232 | if not has_identity: |
| 233 | mapper.dispatch.before_insert(mapper, connection, state) |
| 234 | else: |
| 235 | mapper.dispatch.before_update(mapper, connection, state) |
| 236 | |
| 237 | if mapper._validate_polymorphic_identity: |
| 238 | mapper._validate_polymorphic_identity(mapper, state, dict_) |
| 239 | |
| 240 | # detect if we have a "pending" instance (i.e. has |
| 241 | # no instance_key attached to it), and another instance |
| 242 | # with the same identity key already exists as persistent. |
| 243 | # convert to an UPDATE if so. |
| 244 | if ( |
| 245 | not has_identity |
| 246 | and instance_key in uowtransaction.session.identity_map |
| 247 | ): |
| 248 | instance = uowtransaction.session.identity_map[instance_key] |
| 249 | existing = attributes.instance_state(instance) |
| 250 | |
| 251 | if not uowtransaction.was_already_deleted(existing): |
| 252 | if not uowtransaction.is_deleted(existing): |
| 253 | util.warn( |
| 254 | "New instance %s with identity key %s conflicts " |
| 255 | "with persistent instance %s" |
| 256 | % (state_str(state), instance_key, state_str(existing)) |
| 257 | ) |
| 258 | else: |
| 259 | base_mapper._log_debug( |
| 260 | "detected row switch for identity %s. " |
| 261 | "will update %s, remove %s from " |
| 262 | "transaction", |
| 263 | instance_key, |
| 264 | state_str(state), |
| 265 | state_str(existing), |
| 266 | ) |
| 267 |
no test coverage detected