-
Create a virtual environment:
python -m venv .venv
Activate the virtual environment:
- On macOS/Linux:
source .venv/bin/activate- On Windows use
.venv\Scripts\activate
-
Install PostgreSQL:
-
On macOS, you can use Homebrew:
brew install postgresql
-
On Ubuntu:
sudo apt-get install postgresql postgresql-contrib
-
On Windows, download the installer from the PostgreSQL official site.
-
-
Start PostgreSQL service:
- On macOS:
brew services start postgresql
- On Ubuntu:
sudo service postgresql start
- On Windows, the service should start automatically after installation.
- On macOS:
-
Create a database:
psql -U postgres -c "CREATE DATABASE fastapi_week4;"
- Copy the example environment file:
cp .env.example .env
- Edit the
.envfile:- Set your database user and password in the
.envfile:
DB_USER=your_db_user DB_PASS=your_db_password - Set your database user and password in the
- Install SQLAlchemy:
pip install sqlalchemy dotenv psycopg2
```bash
python main.py
- Objective: Model a one-to-many relationship between
ArticleandComment. - Instructions:
-
In
models/article_comment_onetomany.py, define two SQLAlchemy models:Article:- Fields:
id(primary key),title(required)
- Fields:
- Relationship: One Article has many Comments
Comment:- Fields:
id(primary key),content(required),article_id(foreign key)
- Fields:
- Constraints:
- Use SQLAlchemy's relationship() and ForeignKey() to link the models.
-
Use
declarative_base()to define the ORM base class.
-
- Objective: Create the PostgreSQL connection and initialize the database schema.
- Instructions:
- In
main.py, do the following:- Load environment variables using
dotenv. - Read
DB_USERandDB_PASSfrom .env, with fallback defaults. - Create a connection string:
postgresql://<DB_USER>:<DB_PASS>@localhost/fastapi_week4 - Use
create_engine()to initialize a SQLAlchemy engine. - Use
Base.metadata.create_all()to create tables.
- Load environment variables using
- In
- Objective: Populate the database with sample data.
- Instructions:
- Create a session using sessionmaker.
- Create one Article titled "Sample Article".
- Create two Comment objects with different content.
- Associate the comments with the article using:
article.comments = [comment1, comment2]
- Add and commit the article (with comments) to the session.
- Objective: Retrieve an article and print its related comments.
- Instructions:
- Use a new session to query the first article from the database.
- Print:
- Article ID
- Number of comments
- Each comment's ID
-
models/article_comment_onetomany.pyfrom sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Article(Base): __tablename__ = 'articles' id = Column(Integer, primary_key=True) title = Column(String, nullable=False) comments = relationship("Comment") class Comment(Base): __tablename__ = 'comments' id = Column(Integer, primary_key=True) content = Column(String, nullable=False) article_id = Column(Integer, ForeignKey('articles.id'))
-
main.pyimport os from dotenv import load_dotenv from sqlalchemy import create_engine from models.article_comment_onetomany import Article, Comment from sqlalchemy.orm import sessionmaker from models.article_comment_onetomany import Base load_dotenv() # Read DB_USER and DB_PASS from environment variables DB_USER = os.getenv("DB_USER", "postgres") DB_PASS = os.getenv("DB_PASS", "postgres") print(f"DB_USER: {DB_USER}, DB_PASS: {DB_PASS}") SQLALCHEMY_DATABASE_URL = f'postgresql://{DB_USER}:{DB_PASS}@localhost/fastapi_week4' # Create the SQLAlchemy engine engine = create_engine(SQLALCHEMY_DATABASE_URL) # Create session Session = sessionmaker(autocommit=False, autoflush=False, bind=engine) # Create tables Base.metadata.create_all(bind=engine) with Session() as session: article = Article(title="Sample Article") comment1 = Comment(content="This is the first comment.") comment2 = Comment(content="This is the second comment.") article.comments = [comment1, comment2] session.add(article) session.commit() # Query the article and its comments with Session() as session: article = session.query(Article).first() print(f"Article ID: {article.id}, Number of Comments: {len(article.comments)}") for comment in article.comments: print(f"Comment ID: {comment.id}")
- Run the database app:
python main.py