MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / polymorphic_instance

Function polymorphic_instance

lib/sqlalchemy/orm/loading.py:1495–1531  ·  view source on GitHub ↗
(row)

Source from the content-addressed store, hash-verified

1493 getter = result._getter(polymorphic_on)
1494
1495 def polymorphic_instance(row):
1496 discriminator = getter(row)
1497 if discriminator is not None:
1498 _instance = polymorphic_instances[discriminator]
1499 if _instance:
1500 return _instance(row)
1501 elif _instance is False:
1502 identitykey = ensure_no_pk(row)
1503
1504 if identitykey:
1505 raise sa_exc.InvalidRequestError(
1506 "Row with identity key %s can't be loaded into an "
1507 "object; the polymorphic discriminator column '%s' "
1508 "refers to %s, which is not a sub-mapper of "
1509 "the requested %s"
1510 % (
1511 identitykey,
1512 polymorphic_on,
1513 mapper.polymorphic_map[discriminator],
1514 mapper,
1515 )
1516 )
1517 else:
1518 return None
1519 else:
1520 return instance_fn(row)
1521 else:
1522 identitykey = ensure_no_pk(row)
1523
1524 if identitykey:
1525 raise sa_exc.InvalidRequestError(
1526 "Row with identity key %s can't be loaded into an "
1527 "object; the polymorphic discriminator column '%s' is "
1528 "NULL" % (identitykey, polymorphic_on)
1529 )
1530 else:
1531 return None
1532
1533 return polymorphic_instance
1534

Callers

nothing calls this directly

Calls 2

_instanceFunction · 0.85
ensure_no_pkFunction · 0.85

Tested by

no test coverage detected