2025-03-25 16:19:37 +01:00

33 lines
1.6 KiB
Python

from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
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)
# 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:
- `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.
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.