Add Lake model
This commit is contained in:
parent
0fb10b86a8
commit
f9528ee16d
34
models/lake.py
Normal file
34
models/lake.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
Here's a SQLAlchemy model for the "Lake" entity with the description "generate list of lakes in paris":
|
||||||
|
|
||||||
|
```python
|
||||||
|
from sqlalchemy import Column, String, Text, DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from app.api.db.base_class import Base
|
||||||
|
|
||||||
|
class Lake(Base):
|
||||||
|
__tablename__ = "lakes"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
|
description = Column(Text, nullable=True)
|
||||||
|
location = Column(String, nullable=False)
|
||||||
|
|
||||||
|
created_at = Column(DateTime, server_default=func.now())
|
||||||
|
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Lake(id={self.id}, name='{self.name}', location='{self.location}')"
|
||||||
|
```
|
||||||
|
|
||||||
|
Explanation:
|
||||||
|
|
||||||
|
- `__tablename__` is set to "lakes" for the table name.
|
||||||
|
- `id` is the primary key column using `UUID` from the `postgresql` dialect.
|
||||||
|
- `name` is a required `String` column, unique, and indexed for efficient searches.
|
||||||
|
- `description` is an optional `Text` column for storing longer descriptions.
|
||||||
|
- `location` is a required `String` column for storing the location of the lake.
|
||||||
|
- `created_at` and `updated_at` are `DateTime` columns for tracking when the record was created and last updated, using the database server's default time functions.
|
||||||
|
- `__repr__` is a custom string representation of the object, useful for debugging.
|
||||||
|
|
||||||
|
This model assumes you're using PostgreSQL as the database and have set up the `Base` class correctly. It also assumes you have the necessary imports from SQLAlchemy and the `app.api.db.base_class` module.
|
Loading…
x
Reference in New Issue
Block a user