Overview
The Catalog model represents the generic definition of items available in the library system. It serves as the template or blueprint for physical items, storing information like title, category, and author/brand. Each catalog entry can have multiple physical instances (ItemInstance).
Fields
Primary key identifier for the catalog entry
The title of the book or name of the equipment. Required field with maximum 150 characters.
Category of the item (e.g., ‘libro’, ‘equipo’, ‘revista’). Required field with maximum 50 characters.
Author name for books or brand name for equipment. Optional field with maximum 100 characters.
Relationships
instances
Relationship to all physical instances of this catalog item.
- Type: Dynamic relationship to
ItemInstance model
- Backref:
catalog_item (accessible from ItemInstance)
- Cascade:
all, delete-orphan - deleting a catalog entry removes all its instances
- Access:
catalog.instances.all() or catalog.instances.filter_by(status='disponible')
Usage Examples
Creating a New Catalog Entry
from app.models import Catalog
from app import db
# Create a book catalog entry
book = Catalog(
title_or_name='Clean Code: A Handbook of Agile Software Craftsmanship',
category='libro',
author_or_brand='Robert C. Martin'
)
db.session.add(book)
db.session.commit()
# Create an equipment catalog entry
equipment = Catalog(
title_or_name='Arduino Uno R3',
category='equipo',
author_or_brand='Arduino'
)
db.session.add(equipment)
db.session.commit()
Querying Catalog Items
from app.models import Catalog
# Get all books
books = Catalog.query.filter_by(category='libro').all()
# Search by title
results = Catalog.query.filter(
Catalog.title_or_name.contains('Python')
).all()
# Get catalog with specific author
author_books = Catalog.query.filter_by(
author_or_brand='Robert C. Martin'
).all()
Accessing Instances
catalog = Catalog.query.get(catalog_id)
# Get all instances
all_instances = catalog.instances.all()
# Get available instances
available = catalog.instances.filter_by(status='disponible').all()
# Count total instances
total_count = catalog.instances.count()
available_count = catalog.instances.filter_by(status='disponible').count()
catalog = Catalog.query.get(catalog_id)
catalog.title_or_name = 'Updated Title'
catalog.author_or_brand = 'New Author Name'
db.session.commit()
Deleting a Catalog Entry
catalog = Catalog.query.get(catalog_id)
# This will also delete all associated instances due to cascade
db.session.delete(catalog)
db.session.commit()
Common Query Patterns
from app.models import Catalog, ItemInstance
from sqlalchemy import func
# Get catalogs with instance counts
catalogs = db.session.query(
Catalog,
func.count(ItemInstance.id).label('total_instances')
).outerjoin(ItemInstance).group_by(Catalog.id).all()
for catalog, count in catalogs:
print(f"{catalog.title_or_name}: {count} instances")
Filter by Category
# Get all equipment
equipment_list = Catalog.query.filter_by(category='equipo').all()
# Get all books
book_list = Catalog.query.filter_by(category='libro').all()
Design Pattern
The Catalog model follows the Type-Object pattern:
- Catalog: Defines the “type” or template (generic item information)
- ItemInstance: Represents the “object” or physical manifestation
This allows the library to:
- Maintain single source of truth for item metadata
- Track multiple physical copies of the same item
- Update item information in one place
- Manage availability at the instance level