3. Interview Questions- Relationship Configuration
1. What is the relationship() function used for in SQLAlchemy?
relationship() function used for in SQLAlchemy?2. How is a one-to-many relationship declared in SQLAlchemy ORM?
class User(Base):
addresses: Mapped[List["Address"]] = relationship("Address", back_populates="user")
class Address(Base):
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
user: Mapped["User"] = relationship("User", back_populates="addresses")3. What is the purpose of back_populates in a relationship?
back_populates in a relationship?4. Can you use backref instead of back_populates?
backref instead of back_populates?5. What is the default collection type for one-to-many relationships?
6. How do you define a many-to-one relationship?
7. How do you define a many-to-many relationship in SQLAlchemy?
8. What argument is used in relationship() to define a many-to-many join table?
relationship() to define a many-to-many join table?9. What is the difference between uselist=True and uselist=False?
uselist=True and uselist=False?10. What does lazy="select" mean in a relationship?
lazy="select" mean in a relationship?11. How can you eager load a relationship in SQLAlchemy?
12. What is cascade="all, delete-orphan" used for?
cascade="all, delete-orphan" used for?13. How do you define a one-to-one relationship?
14. How does SQLAlchemy detect the direction of relationships?
15. What is a foreign_keys argument used for in relationship()?
foreign_keys argument used for in relationship()?16. What is an association object pattern in SQLAlchemy?
17. Can you create relationships with composite (multi-column) foreign keys?
18. How do you make a self-referential relationship?
19. What happens if you omit back_populates or backref entirely?
back_populates or backref entirely?20. What is the difference between joined, selectin, and subquery loading strategies?
joined, selectin, and subquery loading strategies?21. What is the primaryjoin argument in relationship() used for?
primaryjoin argument in relationship() used for?22. When should you use the secondaryjoin argument in a relationship?
secondaryjoin argument in a relationship?23. How do you handle a relationship between the same table (e.g., self-referencing)?
24. What does viewonly=True do in a relationship?
viewonly=True do in a relationship?25. What is an association object and how is it different from an association table?
26. How can you define additional columns on a many-to-many relationship?
27. What are the common values for the cascade option in a relationship?
cascade option in a relationship?28. How do you prevent deletion of orphaned child objects?
29. What is the default value of cascade in a relationship?
cascade in a relationship?30. What is the purpose of enable_typechecks=False in relationship()?
enable_typechecks=False in relationship()?31. What does passive_deletes=True do in a relationship?
passive_deletes=True do in a relationship?32. When should you use foreign_keys in self-referential relationships?
foreign_keys in self-referential relationships?33. How does lazy='dynamic' change how relationships behave?
lazy='dynamic' change how relationships behave?34. What does lazy='selectin' do?
lazy='selectin' do?35. What’s the trade-off between lazy='joined' and lazy='selectin'?
lazy='joined' and lazy='selectin'?36. What is the purpose of overlaps in a relationship?
overlaps in a relationship?37. How can you filter a relationship automatically (e.g., only active items)?
38. What happens when you assign a list to a uselist=False relationship?
uselist=False relationship?39. What is collection_class used for in relationships?
collection_class used for in relationships?40. Can a relationship() be defined on both sides of a many-to-many association?
relationship() be defined on both sides of a many-to-many association?41. Can you use a relationship() without a ForeignKey?
relationship() without a ForeignKey?42. What does compare_to do in custom collection classes for relationships?
compare_to do in custom collection classes for relationships?43. What is the purpose of remote_side in relationships?
remote_side in relationships?44. How do selectinload() and joinedload() differ in behavior for collections?
selectinload() and joinedload() differ in behavior for collections?45. Can relationship() be used on columns with composite foreign keys?
relationship() be used on columns with composite foreign keys?46. What happens if a child object is assigned to multiple parents in a one-to-many relationship?
47. How does SQLAlchemy handle circular dependencies in relationships?
48. How can you make sure orphaned related objects are deleted automatically?
49. Is it possible to override a relationship dynamically at runtime?
50. What is active_history=True used for in a relationship?
active_history=True used for in a relationship?Previous2. Interview Questions- ORM Mapped Class ConfigurationNext4. Interview Questions- ORM Querying Guide
Last updated