41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
Here's the SQLAlchemy model for the `Book` entity:
|
|
|
|
```python
|
|
from sqlalchemy import Column, String, Text, 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(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
title = Column(String, nullable=False, index=True)
|
|
description = Column(Text, nullable=True)
|
|
author = Column(String, nullable=False, index=True)
|
|
pages = Column(Integer, nullable=False)
|
|
|
|
# Relationships
|
|
# Example: user_id = Column(String, ForeignKey("users.id"))
|
|
# Example: user = relationship("User", back_populates="books")
|
|
|
|
# Timestamps
|
|
created_at = Column(DateTime, default=func.now())
|
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
|
```
|
|
|
|
This model includes the following columns:
|
|
|
|
- `id`: A UUID primary key column
|
|
- `title`: A required string column for the book title, indexed for faster searches
|
|
- `description`: An optional text column for the book description
|
|
- `author`: A required string column for the author name, indexed for faster searches
|
|
- `pages`: A required integer column for the number of pages
|
|
|
|
Additionally, the model includes:
|
|
|
|
- `created_at` and `updated_at` columns for tracking when the record was created and last updated
|
|
- Placeholders for relationships to other models (e.g., `User`) if needed
|
|
|
|
You can customize this model further by adding or modifying columns, constraints, and relationships based on your specific requirements. |