1. Interview Questions -SQLAlchemy ORM
1. What is the purpose of DeclarativeBase in SQLAlchemy ORM?
DeclarativeBase in SQLAlchemy ORM?2. How do you define a table name in an ORM model?
class User(Base):
__tablename__ = "user_account"3. What import is required to define mapped columns in ORM models?
from sqlalchemy.orm import Mapped, mapped_column4. How do you define a primary key column in a model?
id: Mapped[int] = mapped_column(primary_key=True)5. How can you define a column with a specific string length?
6. What is the advantage of using Mapped over plain type annotations in SQLAlchemy 2.0?
Mapped over plain type annotations in SQLAlchemy 2.0?7. How do you declare a relationship between two models?
8. What is back_populates used for in relationships?
back_populates used for in relationships?9. How do you represent a foreign key in an ORM model?
10. How do you define a string column with no length limit?
11. How do you create the tables defined by ORM models in the database?
12. What does metadata.create_all() do?
metadata.create_all() do?13. How do you configure the base class to use a metadata object?
14. What is the purpose of Mapped[List[...]] in relationship fields?
Mapped[List[...]] in relationship fields?15. Can you use forward declarations for models in relationships?
16. How does SQLAlchemy map columns to class attributes?
17. Is the use of __init__ required when defining ORM models?
__init__ required when defining ORM models?18. What is the role of Base.metadata?
Base.metadata?19. How do you specify a nullable column?
20. How do relationships handle collections by default?
21. What does ForeignKey("user_account.id") refer to in a mapped column?
ForeignKey("user_account.id") refer to in a mapped column?22. Can a model have multiple foreign keys?
23. How do you define a one-to-many relationship in SQLAlchemy?
24. What happens if you omit back_populates in relationships?
back_populates in relationships?25. What’s the difference between backref and back_populates?
backref and back_populates?26. What does the String(30) in a column mean?
String(30) in a column mean?27. Can you define constraints like unique=True on columns?
unique=True on columns?28. How do you define default values for columns in ORM models?
29. How can you enforce non-null constraints in a model field?
30. What data types can you use in mapped_column?
mapped_column?31. What does Mapped[int] indicate to SQLAlchemy?
Mapped[int] indicate to SQLAlchemy?32. How are Python classes linked to SQL tables in SQLAlchemy ORM?
33. What would happen if two models used the same __tablename__?
__tablename__?34. Can you mix traditional SQLAlchemy Core with ORM?
35. How does SQLAlchemy handle pluralization or table name inference?
36. What is the default collection type for relationships in SQLAlchemy ORM?
37. What is the purpose of the DeclarativeBase metaclass?
DeclarativeBase metaclass?38. Can you extend multiple base classes for models in SQLAlchemy ORM?
39. Why are string-based annotations (e.g., "Address") used in relationships?
"Address") used in relationships?40. What does Base.metadata.create_all(engine) actually use to build the schema?
Base.metadata.create_all(engine) actually use to build the schema?41. Can you define a composite primary key using DeclarativeBase?
DeclarativeBase?42. How do you map a class without defining __tablename__?
__tablename__?43. What happens if Mapped is not used in column type hints?
Mapped is not used in column type hints?44. How can you access all defined tables in your ORM setup?
45. Is it necessary to use relationship() on both sides of a relation?
relationship() on both sides of a relation?46. How do you handle circular imports in relationship declarations?
47. Can Mapped be used with generic types like Optional or List?
Mapped be used with generic types like Optional or List?48. How do you avoid automatic pluralization of relationships?
49. What does the __init__ method do in SQLAlchemy ORM models?
__init__ method do in SQLAlchemy ORM models?50. How do you associate a model with an existing table that was already created in the DB?
Last updated