MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_no_load_preserves_parents

Method test_no_load_preserves_parents

test/orm/test_merge.py:1285–1377  ·  view source on GitHub ↗

Merge with load=False does not trigger a 'delete-orphan' operation. merge with load=False sets attributes without using events. this means the 'hasparent' flag is not propagated to the newly merged instance. in fact this works out OK, because the '_state.pare

(self)

Source from the content-addressed store, hash-verified

1283 self.assert_sql_count(testing.db, go, 0)
1284
1285 def test_no_load_preserves_parents(self):
1286 """Merge with load=False does not trigger a 'delete-orphan'
1287 operation.
1288
1289 merge with load=False sets attributes without using events.
1290 this means the 'hasparent' flag is not propagated to the newly
1291 merged instance. in fact this works out OK, because the
1292 '_state.parents' collection on the newly merged instance is
1293 empty; since the mapper doesn't see an active 'False' setting in
1294 this collection when _is_orphan() is called, it does not count
1295 as an orphan (i.e. this is the 'optimistic' logic in
1296 mapper._is_orphan().)
1297
1298 """
1299
1300 users, Address, addresses, User = (
1301 self.tables.users,
1302 self.classes.Address,
1303 self.tables.addresses,
1304 self.classes.User,
1305 )
1306
1307 self.mapper_registry.map_imperatively(
1308 User,
1309 users,
1310 properties={
1311 "addresses": relationship(
1312 self.mapper_registry.map_imperatively(Address, addresses),
1313 backref="user",
1314 cascade="all, delete-orphan",
1315 )
1316 },
1317 )
1318 with fixture_session(expire_on_commit=False) as sess:
1319 u = User()
1320 u.id = 7
1321 u.name = "fred"
1322 a1 = Address()
1323 a1.email_address = "foo@bar.com"
1324 u.addresses.append(a1)
1325 sess.add(u)
1326 sess.commit()
1327
1328 assert u.addresses[0].user is u
1329
1330 with fixture_session(expire_on_commit=False) as sess2:
1331 u2 = sess2.merge(u, load=False)
1332 assert not sess2.dirty
1333 a2 = u2.addresses[0]
1334 a2.email_address = "somenewaddress"
1335 assert not sa.orm.object_mapper(a2)._is_orphan(
1336 sa.orm.attributes.instance_state(a2)
1337 )
1338 sess2.commit()
1339
1340 with fixture_session() as sess2:
1341 eq_(
1342 sess2.get(User, u2.id).addresses[0].email_address,

Callers

nothing calls this directly

Calls 14

relationshipFunction · 0.90
fixture_sessionFunction · 0.90
eq_Function · 0.90
map_imperativelyMethod · 0.80
_is_orphanMethod · 0.80
UserClass · 0.70
AddressClass · 0.70
appendMethod · 0.45
addMethod · 0.45
commitMethod · 0.45
mergeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected