From f81f5f8cf37a9f681ae68055a710b71de6fb3d8f Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 18:39:39 +0100 Subject: [PATCH] Add Book model --- models/book.py | 41 ++++++----------------------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/models/book.py b/models/book.py index 4425259..81dc46c 100644 --- a/models/book.py +++ b/models/book.py @@ -1,46 +1,17 @@ -from sqlalchemy import Column, String, ForeignKey +from sqlalchemy import Column, String, Integer, DateTime, ForeignKey from sqlalchemy.orm import relationship -from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func from app.api.db.base_class import Base import uuid class Book(Base): __tablename__ = "books" - id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column(Integer, primary_key=True, index=True) title = Column(String, nullable=False, index=True) author = Column(String, nullable=False, index=True) description = Column(String, nullable=True) - - # Relationships - # author_id = Column(UUID(as_uuid=True), ForeignKey('authors.id')) - # author = relationship("Author", back_populates="books") - + # Timestamps - created_at = Column( - "created_at", - TIMESTAMP(timezone=True), - nullable=False, - server_default=func.now(), - ) - updated_at = Column( - "updated_at", - TIMESTAMP(timezone=True), - nullable=False, - server_default=func.now(), - onupdate=func.now(), - ) -``` - -This SQLAlchemy model defines a `Book` class that extends the `Base` class. It includes the following columns: - -- `id`: A UUID primary key column for the book. -- `title`: A string column for the book's title, which is non-nullable and indexed for efficient filtering. -- `author`: A string column for the book's author, which is non-nullable and indexed for efficient filtering. -- `description`: An optional string column for the book's description. - -The model also includes two timestamp columns, `created_at` and `updated_at`, which automatically record the creation and update times for each book record. - -Additionally, there is a commented-out section for a potential one-to-many relationship between `Book` and `Author` models. If you need to associate books with authors, you can uncomment this section and create the `Author` model accordingly. - -This model should work with FastAPI and Alembic migrations, allowing you to create and manage book records in your database while providing efficient filtering capabilities based on the author and title columns. \ No newline at end of file + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file