5. Interview Questions- Using the Session
1. What is the purpose of the Session in SQLAlchemy ORM?
Session in SQLAlchemy ORM?2. How do you create a new session in SQLAlchemy 2.0?
from sqlalchemy.orm import Session
with Session(engine) as session:
...3. What is the benefit of using a with block for Session?
with block for Session?4. How do you persist a new object using the session?
new_user = User(name="Alice")
session.add(new_user)
session.commit()5. What’s the difference between add() and add_all() in a session?
add() and add_all() in a session?6. What does session.commit() do?
session.commit() do?7. What’s the difference between session.commit() and session.flush()?
session.commit() and session.flush()?8. What does session.rollback() do?
session.rollback() do?9. What happens if you close a session with session.close()?
session.close()?10. What is the difference between a “dirty” and a “new” object in the session?
11. How do you delete an object from the database?
12. What is expire_on_commit=True and when should you disable it?
expire_on_commit=True and when should you disable it?13. How can you inspect the state of the session?
14. What happens if you access a flushed but uncommitted object’s attribute?
15. Can you reuse a session across multiple requests in a web app?
16. How do you refresh an object with values from the DB?
17. What is the autoflush parameter in a session?
autoflush parameter in a session?18. How do you merge a detached instance back into a session?
19. What does session.get(Model, primary_key) do?
session.get(Model, primary_key) do?20. What is the binds parameter in session configuration?
binds parameter in session configuration?21. What is the identity map in a SQLAlchemy session?
22. What happens when you query the same object twice within one session?
23. How do you remove an object from the session without deleting it from the database?
24. What does session.clear() do?
session.clear() do?25. What is a detached object in SQLAlchemy?
26. What happens if you modify a detached object and call session.commit()?
session.commit()?27. How can you tell if an object is persistent, pending, or detached?
28. What’s the difference between Session.commit() and Session.flush() in terms of transaction boundaries?
Session.commit() and Session.flush() in terms of transaction boundaries?29. Can you roll back only part of a session's changes?
30. What is a sessionmaker and why is it useful?
31. What is the difference between Session() and sessionmaker()?
Session() and sessionmaker()?32. How do you manually bind a Session to a specific engine?
Session to a specific engine?33. What’s the recommended practice for session management in web apps?
34. Can a session span multiple transactions?
35. What happens if you try to use a session after it's been closed?
36. What is autocommit and should it be used?
autocommit and should it be used?37. Can multiple threads share the same session instance?
38. How does Session.begin() differ from using with Session()?
Session.begin() differ from using with Session()?39. How do you execute raw SQL inside a session?
40. What’s the difference between commit() and flush() when using add()?
commit() and flush() when using add()?41. What does Session.begin() do?
Session.begin() do?42. Can you nest transactions in SQLAlchemy ORM sessions?
43. What is the use of Session.in_transaction()?
Session.in_transaction()?44. What is the Session.get_bind() method used for?
Session.get_bind() method used for?45. How does SQLAlchemy determine which engine to use for a query?
46. Can you change the bind for a session at runtime?
47. What does Session.is_active represent?
Session.is_active represent?48. What happens if an exception occurs during a session’s transaction block?
49. When should you use Session.rollback() explicitly?
Session.rollback() explicitly?50. Why is using with Session(engine) as session: considered best practice?
with Session(engine) as session: considered best practice?Last updated