02: Changed setup for alembic and sqlAlchemy

This commit is contained in:
exolonConfidental
2026-02-06 22:22:59 +05:30
parent 61162e01fb
commit 8fb3b7cf67
14 changed files with 739 additions and 25 deletions

5
app/db/models/base.py Normal file
View File

@@ -0,0 +1,5 @@
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass

46
app/db/models/location.py Normal file
View File

@@ -0,0 +1,46 @@
from sqlalchemy import String, Integer, Float, BigInteger
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Mapped, mapped_column
from app.db.models.base import Base
class Location(Base):
__tablename__ = "locations"
id: Mapped[int] = mapped_column(primary_key=True, index=True)
# OSM Identifiers
place_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
osm_id: Mapped[int] = mapped_column(BigInteger)
osm_type: Mapped[str] = mapped_column(String(20))
# Coordinates
latitude: Mapped[float] = mapped_column(Float)
longitude: Mapped[float] = mapped_column(Float)
# Address Info
house_number: Mapped[str] = mapped_column(String(20), nullable=True)
road: Mapped[str] = mapped_column(String(150))
city: Mapped[str] = mapped_column(String(100))
county: Mapped[str] = mapped_column(String(100))
state: Mapped[str] = mapped_column(String(100))
postcode: Mapped[str] = mapped_column(String(20))
country: Mapped[str] = mapped_column(String(100))
country_code: Mapped[str] = mapped_column(String(10))
# Display name
display_name: Mapped[str] = mapped_column(String(300))
# Bounding Box
bbox_lat_min: Mapped[float] = mapped_column(Float)
bbox_lat_max: Mapped[float] = mapped_column(Float)
bbox_lon_min: Mapped[float] = mapped_column(Float)
bbox_lon_max: Mapped[float] = mapped_column(Float)
property = relationship(
"Property",
back_populates="location",
uselist=False,
cascade="all, delete-orphan"
)

31
app/db/models/owner.py Normal file
View File

@@ -0,0 +1,31 @@
from sqlalchemy import String, Integer
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.models.base import Base
class Owner(Base):
__tablename__ = "owners"
id: Mapped[int] = mapped_column(primary_key=True)
full_name: Mapped[str] = mapped_column(String(150))
phone_number: Mapped[str] = mapped_column(String(20), unique=True)
email: Mapped[str] = mapped_column(String(150), unique=True)
occupation: Mapped[str] = mapped_column(String(100))
annual_income_range: Mapped[str] = mapped_column(String(50))
willing_to_rent: Mapped[bool] = mapped_column(default=False)
desired_rent_price: Mapped[int] = mapped_column(nullable=True)
willing_to_sell: Mapped[bool] = mapped_column(default=False)
desired_sell_price: Mapped[int] = mapped_column(nullable=True)
# Relationship
properties = relationship(
"Property",
back_populates="owner",
cascade="all, delete"
)

65
app/db/models/property.py Normal file
View File

@@ -0,0 +1,65 @@
from sqlalchemy import String, Integer, Float, Date, Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.models.base import Base
from datetime import date
class Property(Base):
__tablename__ = "properties"
id: Mapped[int] = mapped_column(primary_key=True)
# Basic Info
property_type: Mapped[str] = mapped_column(String(50))
built_up_area: Mapped[float] = mapped_column(Float)
bedrooms: Mapped[int] = mapped_column(Integer)
bathrooms: Mapped[int] = mapped_column(Integer)
# Transaction Info
purchase_date: Mapped[date] = mapped_column(Date)
purchase_price: Mapped[float]
sale_listing_date: Mapped[date] = mapped_column(Date,nullable=True)
sale_asking_price: Mapped[float] = mapped_column(nullable=True)
# Maintenance
last_renovation_date: Mapped[date] = mapped_column(Date,nullable=True)
renovation_description: Mapped[str] = mapped_column(String(255), nullable=True)
roof_repair_date: Mapped[date] = mapped_column(Date,nullable=True)
roof_condition: Mapped[str] = mapped_column(String(50))
# Rental Status
available_for_rent: Mapped[bool] = mapped_column(Boolean, default=False)
expected_rent: Mapped[float] = mapped_column(nullable=True)
rental_available_date: Mapped[date] = mapped_column(Date, nullable=True)
# -------------------------
# Foreign Keys
# -------------------------
owner_id: Mapped[int] = mapped_column(
ForeignKey("owners.id", ondelete="CASCADE")
)
location_id: Mapped[int] = mapped_column(
ForeignKey("locations.id", ondelete="CASCADE"),
unique=True # ONE property = ONE location
)
# -------------------------
# Relationships
# -------------------------
owner = relationship(
"Owner",
back_populates="properties"
)
location = relationship (
"Location",
back_populates="property",
uselist=False,
)