Skip to content

Jorge-de-la-Flor/sqlalchemy-orm-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README in Spanish

SQLAlchemy Advanced Operations

Architected bidirectional one-to-many relationships with lazy loading and cascade operations. Implemented complex aggregation queries using func.count(), group_by(), and subqueries. Designed session management with proper commit and rollback transaction handling. Built a data analysis pipeline with status distribution, category-based metrics, and trend analysis. Automated report generation with JSON serialization and metadata tagging. Tech stack: SQLAlchemy 2.0+, Python 3.14, SQLite3.

Advanced ORM demonstrations showcasing professional database operations, relationships, and complex queries using SQLAlchemy 2.0+.

Features

  • Relational Models: Definition of BusinessOperation and OperationDetail entities with bidirectional one-to-many relationships.

  • Full CRUD: Create, read, and update operations with exception and transaction handling.

  • Advanced Queries: Filtering by type, retrieval with relationships, and complex aggregations.

  • Dynamic Summaries: Generation of statistics on operations (sum, average, max, min).

  • Data Cleansing: Function to delete old records by date.

  • Session Management: Proper handling of sessions, commits, and rollbacks.

Technical Stack

  • SQLAlchemy 2.0+ – ORM y query builder.
  • SQLite – Lightweight, configurable database.
  • Python 3.14+

Main Features

  • create_operation() – Create an operation with associated details.
  • get_operations_by_type() – Filter operations by type.
  • get_operation_with_details() – Get an operation with loaded relationships.
  • update_operation_status() – Update the status with automatic timestamp.
  • get_operations_summary() – Generate a summary with statistics and distributions.
  • cleanup_old_operations() – Delete old records based on age.

Quick Start

from main import DatabaseManager

# Initialize
db = DatabaseManager()

# Create operation
operation = db.create_operation(
    "OP_001", 
    "logistics", 
    1250.50,
    details={"priority": "high", "location": "Lima"}
)

# Get summary
summary = db.get_operations_summary()
print(f"Total operaciones: {summary['total_operations']}")

# Update status
db.update_operation_status(operation.id, "completed")

# Log out
db.close()

Installation and Execution

uv sync
uv run python sqlalchemy_operations.py
uv run pytest -v

About

Este repositorio forma parte de mi portafolio personal

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages