6. Interview Questions- Events and Internals
1. What is the purpose of extending the SQLAlchemy ORM?
2. What are ORM events in SQLAlchemy?
3. How do you register an event for when a class is loaded from the database?
from sqlalchemy import event
@event.listens_for(MyClass, "load")
def receive_load(instance, context):
print(f"{instance} was loaded")4. What does the @event.listens_for(..., 'init') decorator do?
@event.listens_for(..., 'init') decorator do?5. What’s the difference between load and refresh events?
load and refresh events?6. How can you track changes to a particular attribute?
7. What event is used to intercept before insert or update in ORM?
8. Can you intercept attribute-level access (e.g., __get__, __set__) using ORM tools?
__get__, __set__) using ORM tools?9. What does the "append" event on a relationship do?
"append" event on a relationship do?10. How can you perform actions when an object is added to a relationship?
11. What is the use of __init__() vs. @event.listens_for(..., 'init')?
__init__() vs. @event.listens_for(..., 'init')?12. How do you extend a mapped class with mixins in SQLAlchemy?
13. What is the difference between a mixin and an abstract base in SQLAlchemy ORM?
14. Can attribute-level events be registered after class definition?
15. How do you track when a relationship collection is cleared?
16. How do you prevent a certain value from being assigned to a field?
17. What is a class manager in SQLAlchemy ORM internals?
18. How does SQLAlchemy know which attributes are mapped and instrumented?
19. Can you define global listeners that affect all ORM classes?
20. What’s the difference between instance-level and class-level event listeners?
21. How do you intercept an object being marked as deleted?
22. What event can you use to automatically populate a timestamp on insert?
23. How do you instrument a class without using declarative?
24. What’s the use of @event.listens_for(Mapper, 'after_configured')?
@event.listens_for(Mapper, 'after_configured')?25. What does the "append" event emit in terms of arguments?
"append" event emit in terms of arguments?26. Can you listen to events on unmapped classes?
27. What’s the difference between "set" and "modified" events on an attribute?
"set" and "modified" events on an attribute?28. How do you dynamically add a listener at runtime?
29. What is the init_scalar event used for?
init_scalar event used for?30. How can you track whether a particular column was changed during a session?
31. How do you mark a mixin as declarative-safe?
32. What does @declarative_mixin offer over a plain class?
@declarative_mixin offer over a plain class?33. Can @event.listens_for be used on hybrid properties?
@event.listens_for be used on hybrid properties?34. How do you register an event on all mappers globally?
35. What is the initiator argument in attribute events?
36. Can listeners be removed once added?
37. What’s the purpose of dispatch on ORM classes or attributes?
dispatch on ORM classes or attributes?38. How can you use ORM events to implement soft deletes?
39. How can you override attribute access behavior in an ORM-mapped class?
40. What is the role of the MapperEvents class in SQLAlchemy?
MapperEvents class in SQLAlchemy?41. What is the purpose of __declare_last__() in a mapped class?
__declare_last__() in a mapped class?42. Can you override default behavior of an attribute (e.g., validation) using events?
43. What’s the role of MapperExtension in SQLAlchemy?
MapperExtension in SQLAlchemy?44. Can @event.listens_for be applied to both attributes and relationships?
@event.listens_for be applied to both attributes and relationships?45. What event should you use to modify values coming from the database during object load?
46. How do you make a declarative mixin class that contributes indexes or constraints?
47. How can you use events to inject behavior across multiple ORM classes?
48. What’s the purpose of the bulk_replace event on relationships?
bulk_replace event on relationships?49. Can you attach listeners to hybrid or computed properties?
50. How does SQLAlchemy handle multiple mixins with overlapping attributes?
Last updated