MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _configure_class_instrumentation

Method _configure_class_instrumentation

lib/sqlalchemy/orm/mapper.py:1470–1561  ·  view source on GitHub ↗

If this mapper is to be a primary mapper (i.e. the non_primary flag is not set), associate this Mapper with the given class and entity name. Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity`` name combination will return this mapper. Also decorat

(self)

Source from the content-addressed store, hash-verified

1468 self._configure_polymorphic_setter(True)
1469
1470 def _configure_class_instrumentation(self):
1471 """If this mapper is to be a primary mapper (i.e. the
1472 non_primary flag is not set), associate this Mapper with the
1473 given class and entity name.
1474
1475 Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity``
1476 name combination will return this mapper. Also decorate the
1477 `__init__` method on the mapped class to include optional
1478 auto-session attachment logic.
1479
1480 """
1481
1482 # we expect that declarative has applied the class manager
1483 # already and set up a registry. if this is None,
1484 # this raises as of 2.0.
1485 manager = attributes.opt_manager_of_class(self.class_)
1486
1487 if self.non_primary:
1488 if not manager or not manager.is_mapped:
1489 raise sa_exc.InvalidRequestError(
1490 "Class %s has no primary mapper configured. Configure "
1491 "a primary mapper first before setting up a non primary "
1492 "Mapper." % self.class_
1493 )
1494 self.class_manager = manager
1495
1496 assert manager.registry is not None
1497 self.registry = manager.registry
1498 self._identity_class = manager.mapper._identity_class
1499 manager.registry._add_non_primary_mapper(self)
1500 return
1501
1502 if manager is None or not manager.registry:
1503 raise sa_exc.InvalidRequestError(
1504 "The _mapper() function and Mapper() constructor may not be "
1505 "invoked directly outside of a declarative registry."
1506 " Please use the sqlalchemy.orm.registry.map_imperatively() "
1507 "function for a classical mapping."
1508 )
1509
1510 self.dispatch.instrument_class(self, self.class_)
1511
1512 # this invokes the class_instrument event and sets up
1513 # the __init__ method. documented behavior is that this must
1514 # occur after the instrument_class event above.
1515 # yes two events with the same two words reversed and different APIs.
1516 # :(
1517
1518 manager = instrumentation.register_class(
1519 self.class_,
1520 mapper=self,
1521 expired_attribute_loader=util.partial(
1522 loading.load_scalar_attributes, self
1523 ),
1524 # finalize flag means instrument the __init__ method
1525 # and call the class_instrument event
1526 finalize=True,
1527 )

Callers 1

__init__Method · 0.95

Calls 5

opt_manager_of_classMethod · 0.80
instrument_classMethod · 0.45
listenMethod · 0.45
unionMethod · 0.45

Tested by

no test coverage detected