Map db table¶
In SQLAlchemy, the declarative_base class is used to create a base class for declarative class definitions. Declarative classes are used to define database tables and their associated mapped classes in a more concise and expressive way. The link between the declarative class and the actual database table is established through a class attribute called __tablename__ and the metadata associated with the declarative_base instance.
Here's a step-by-step explanation:
Create a
declarative_baseinstance: First, you need to create adeclarative_baseinstance. This instance is a factory for declarative base classes and will also hold the metadata about the tables.Define a Declarative Class: Define a declarative class by inheriting from the
Baseclass created in step 1. Within this class, define the table columns as class attributes, and specify the table name using the__tablename__attribute.Table Creation: Once you have defined your declarative class, you can use the
create_all()method on theBaseinstance to create the associated tables in the database.
Now, the User class is linked to the 'users' table in the database. SQLAlchemy uses the information provided in the declarative class (such as column types and constraints) to generate the appropriate SQL statements for table creation.
When you perform queries or insert/update records using instances of the User class, SQLAlchemy knows how to map the class attributes to the corresponding database columns, establishing a connection between your Python code and the database table.
Here's an example of how you might use the declarative class to add a user to the database:
from sqlalchemy.orm import sessionmaker
# Create a session to interact with the database
Session = sessionmaker(bind=engine)
session = Session()
# Create a new user
new_user = User(username='john_doe', email='john@example.com')
# Add the user to the session and commit to the database
session.add(new_user)
session.commit()
In this example, the new_user instance is associated with the 'users' table, and the changes are committed to the database through the SQLAlchemy session.