mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-30 13:10:47 +00:00
define models and bulk insert function
This commit is contained in:
parent
3874da6177
commit
4ad200ea76
2 changed files with 107 additions and 0 deletions
12
backend/app/db/functions/condition_functions.py
Normal file
12
backend/app/db/functions/condition_functions.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from typing import List
|
||||||
|
from sqlalchemy import insert, delete
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from backend.app.db.connection import db_session, db_read_session
|
||||||
|
from backend.app.db.models.condition import PropertyConditionSurveyModel
|
||||||
|
|
||||||
|
|
||||||
|
def bulk_insert_property_surveys(
|
||||||
|
session: Session, surveys: List[PropertyConditionSurveyModel]
|
||||||
|
) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
95
backend/app/db/models/condition.py
Normal file
95
backend/app/db/models/condition.py
Normal file
|
|
@ -0,0 +1,95 @@
|
||||||
|
from sqlalchemy import (
|
||||||
|
BigInteger,
|
||||||
|
Column,
|
||||||
|
Date,
|
||||||
|
ForeignKey,
|
||||||
|
Integer,
|
||||||
|
String,
|
||||||
|
Enum as SqlEnum,
|
||||||
|
)
|
||||||
|
from sqlalchemy.orm import declarative_base, relationship
|
||||||
|
|
||||||
|
from backend.condition.domain.aspect_type import AspectType
|
||||||
|
from backend.condition.domain.element_type import ElementType
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
ElementTypeDb = SqlEnum(
|
||||||
|
ElementType,
|
||||||
|
name="element_type",
|
||||||
|
native_enum=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
AspectTypeDb = SqlEnum(
|
||||||
|
AspectType,
|
||||||
|
name="aspect_type",
|
||||||
|
native_enum=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PropertyConditionSurveyModel(Base):
|
||||||
|
__tablename__ = "property_condition_survey"
|
||||||
|
|
||||||
|
id = Column(BigInteger, primary_key=True, autoincrement=True)
|
||||||
|
uprn = Column(BigInteger, nullable=False)
|
||||||
|
|
||||||
|
date = Column(Date, nullable=False)
|
||||||
|
source = Column(String, nullable=False)
|
||||||
|
|
||||||
|
elements = relationship(
|
||||||
|
"ElementModel",
|
||||||
|
back_populates="survey",
|
||||||
|
cascade="all, delete-orphan",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ElementModel(Base):
|
||||||
|
__tablename__ = "element"
|
||||||
|
|
||||||
|
id = Column(BigInteger, primary_key=True, autoincrement=True)
|
||||||
|
|
||||||
|
survey_id = Column(
|
||||||
|
BigInteger,
|
||||||
|
ForeignKey("property_condition_survey.id"),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
element_type = Column(ElementTypeDb, nullable=False)
|
||||||
|
element_instance = Column(BigInteger, nullable=False)
|
||||||
|
|
||||||
|
survey = relationship(
|
||||||
|
"PropertyConditionSurveyModel",
|
||||||
|
back_populates="elements",
|
||||||
|
)
|
||||||
|
|
||||||
|
aspect_conditions = relationship(
|
||||||
|
"AspectConditionModel",
|
||||||
|
back_populates="element",
|
||||||
|
cascade="all, delete-orphan",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AspectConditionModel(Base):
|
||||||
|
__tablename__ = "aspect_condition"
|
||||||
|
|
||||||
|
id = Column(BigInteger, primary_key=True, autoincrement=True)
|
||||||
|
|
||||||
|
element_id = Column(
|
||||||
|
BigInteger,
|
||||||
|
ForeignKey("element.id"),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
aspect_type = Column(AspectTypeDb, nullable=False)
|
||||||
|
aspect_instance = Column(BigInteger, nullable=False)
|
||||||
|
|
||||||
|
value = Column(String)
|
||||||
|
quantity = Column(Integer)
|
||||||
|
install_date = Column(Date)
|
||||||
|
renewal_year = Column(Integer)
|
||||||
|
comments = Column(String)
|
||||||
|
|
||||||
|
element = relationship(
|
||||||
|
"ElementModel",
|
||||||
|
back_populates="aspect_conditions",
|
||||||
|
)
|
||||||
Loading…
Add table
Reference in a new issue