This project demonstrates the setup and use of SQLAlchemy ORM in a Python application, using Poetry for dependency management and SQLite as the database. It is designed to showcase a clean architecture for Python applications, separating database configuration, models, and CRUD operations for better maintainability and scalability.
- Project Name: SQL Alchemy ORM
- Author: Khursheed Gaddi
- Version: 1.0.0
- License: MIT
Poetry is used as the dependency manager for this project. It simplifies managing dependencies and virtual environments.
First, install Poetry if you don’t have it:
curl -sSL https://install.python-poetry.org | python3 -Alternatively, you can follow the official installation guide for other methods.
To set up the project with Poetry, use the following command in the project directory:
poetry initThis will prompt you to define the project details. Here is a quick setup:
- Package Name:
sqlalchemy_orm - Version:
1.0.0 - Description: A Python project showcasing SQLAlchemy ORM with Poetry and SQLite
- Author:
Khursheed Gaddi <your-email@example.com> - License:
MIT - Dependencies:
SQLAlchemy
Once initialized, install the dependencies:
poetry installYou can manage all project dependencies in the pyproject.toml file that Poetry generates when you initialize the project.
To add a new package, use the following command:
poetry add <package-name>For example, to add SQLAlchemy:
poetry add sqlalchemyThis will automatically update the pyproject.toml file. You can see the list of dependencies in this file under the [tool.poetry.dependencies] section.
Once the environment is set up, you can activate the virtual environment by running:
poetry shellIf you need to run commands without activating the shell, you can prefix them with poetry run:
poetry run python script.pyClone the project repository from GitHub:
git clone https://github.com/khursheed33/sql-alchemy-orm.git
cd sql-alchemy-ormOnce inside the project directory, run the following command to install all the dependencies:
poetry installThis will install all the necessary packages defined in pyproject.toml and set up a virtual environment.
To run the project, use the following commands:
poetry shell # To activate the virtual environment
python sqlalchemy_project/main.py # Run the main fileThis will execute the example code and demonstrate CRUD operations with SQLAlchemy.
After running the project, a SQLite database file (test.db) will be created in the project directory. You can inspect this database using any SQLite client or directly via the command line.
After running the project, you can expect the following output:
Created User: Khursheed, khursheed@example.com
User: Khursheed, khursheed@example.com
Item: Item 1, First item description
Item: Item 2, Second item description
-
Creating a User
You can create a new user using the following function in the code:
create_user(db, name="Alice", email="alice@example.com")
Expected Output:
Created User: Alice, alice@example.com -
Adding Items for a User
To add items for a specific user:
create_item_for_user(db, title="Task 1", description="Complete the report", user_id=user.id)
Expected Output:
Item: Task 1, Complete the report -
Fetching Items for a User
You can retrieve all items associated with a user:
items = get_items_for_user(db, user_id=user.id)
Expected Output:
Item: Task 1, Complete the report
This project is licensed under the MIT License. See the LICENSE file for more details.