#%% # create_engine: This function creates a new SQLAlchemy “engine,” # which is an interface to the database. It acts as the core connection to # your database and manages the communication between your Python code and the database. from sqlalchemy import create_engine # declarative_base: This function is used to create a base class for our ORM models. # All of your database table classes (models) will inherit from this base class. # This base class also ties each model to a corresponding table in the database. from sqlalchemy.ext.declarative import declarative_base # sessionmaker: This is a factory function for creating new Session objects. # Sessions are used to manage the operations (queries, updates, etc.) on # the database in a transaction-safe way. They provide an interface for # interacting with the database. from sqlalchemy.orm import sessionmaker # This line defines the URL for your database connection. # SQLAlchemy uses this URL to determine what type of database you’re # connecting to, and where it’s located. # sqlite:// tells SQLAlchemy that you are using SQLite as the database engine. # ./test.db specifies the relative path to the database file (test.db) in the # current directory (./). SQLite stores the entire database as a single file on disk SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # This line creates the database engine by passing the SQLALCHEMY_DATABASE_URL to the # create_engine function. The engine is responsible for managing the connection to the database. # connect_args={"check_same_thread": False}: This argument is specific to SQLite. By default, # SQLite does not allow multiple threads to interact with the database. The check_same_thread # argument disables this check, allowing the engine to be used in a multi-threaded environment. # This is necessary for many web applications (like FastAPI) that might have multiple requests # hitting the database simultaneously. engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} ) # you create a factory for database sessions. SessionLocal will be used to create individual sessions, # which are needed to interact with the database (querying data, inserting/updating records, etc.). # 1. autocommit=False: This means that changes (inserts, updates, deletes) to the database will not be # committed automatically. You will need to explicitly commit transactions using session.commit(). # This gives you better control over when data is saved. # 2. autoflush=False: This disables automatic flushing. Flushing is the process of sending any pending # changes to the database before executing queries. With autoflush=False, the session will not # automatically send updates to the database unless you explicitly tell it to by calling flush() or commit(). # It prevents unexpected database updates. # 3. bind=engine: This ties the session to the database engine. Any session created with SessionLocal() # will use the engine to communicate with the database. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # This line creates a base class for all of your ORM models (i.e., classes that represent database tables). # Each model (class) will inherit from Base, and SQLAlchemy will use this base class to generate the # necessary SQL statements to create tables and handle CRUD operations (Create, Read, Update, Delete). Base = declarative_base() # %%