Add Book model

This commit is contained in:
Backend IM Bot 2025-03-25 16:28:51 +01:00
parent 9a45e804e0
commit ff663d7e95

View File

@ -1,33 +1,51 @@
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from sqlalchemy.dialects.postgresql import UUID
from app.api.db.base_class import Base
import uuid
class Book(Base):
__tablename__ = "books"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
author = Column(String, index=True)
description = Column(String)
published_year = Column(Integer)
isbn = Column(String, unique=True)
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title = Column(String, nullable=False, index=True)
author = Column(String, nullable=False, index=True)
# Relationships
# author_id = Column(UUID(as_uuid=True), ForeignKey("authors.id"))
# author = relationship("Author", back_populates="books")
# Timestamps
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
```
This SQLAlchemy model defines a `Book` class that extends the `Base` class. It includes the following columns:
This SQLAlchemy model defines a `Book` class with the following columns:
- `id`: An integer primary key with an index.
- `title`: A string column for the book title with an index.
- `author`: A string column for the book author with an index.
- `description`: A string column for the book description.
- `published_year`: An integer column for the year the book was published.
- `isbn`: A string column for the book's ISBN number, which is set as unique.
- `created_at`: A datetime column that stores the creation timestamp, defaulting to the current time.
- `updated_at`: A datetime column that stores the last update timestamp, defaulting to the current time and updating on each change.
- `id` (UUID): The primary key for the book, generated using `uuid.uuid4`.
- `title` (String): The title of the book, indexed for efficient filtering.
- `author` (String): The author of the book, indexed for efficient filtering.
With this model, you can query books by title and author using the indexes on those columns. The `isbn` column is set as unique to prevent duplicates. The timestamps will automatically record when a book record was created and last updated.
The model also includes timestamps for `created_at` and `updated_at` to track when the book record was created and last updated.
You can optionally include a relationship to an `Author` model by uncommenting the `author_id` and `author` lines. In this case, you would need to create a separate `Author` model and define the relationship in both directions.
With this model, you can filter books by `title` and `author` using SQLAlchemy queries. For example:
```python
from app.models import Book
# Filter by title
books = Book.query.filter(Book.title.ilike(f"%{title_filter}%")).all()
# Filter by author
books = Book.query.filter(Book.author.ilike(f"%{author_filter}%")).all()
# Filter by both title and author
books = Book.query.filter(
Book.title.ilike(f"%{title_filter}%"),
Book.author.ilike(f"%{author_filter}%")
).all()
```
Remember to import the necessary modules and configure the database connection in your FastAPI application.