Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
from typing import Any, TypeVar, Dict
from sqlalchemy.orm import (
Mapped,
RelationshipProperty,
mapped_column as column,
Session,
)
from sqlalchemy.sql.functions import now
from sqlalchemy.exc import IntegrityError
from utils.constants import TIME
from utils.functions import camelToSnake
from utils.db import Base
T = TypeVar("T", bound="BaseModel")
class BaseModel(Base):
__abstract__: bool = True
__allow_unmapped__: bool = True
__tablename__: str
created: Mapped[TIME] = column(server_default=now())
updated: Mapped[TIME] = column(server_default=now(), onupdate=now())
comments: Mapped[str | None]
@classmethod
def __init_subclass__(cls, **kwargs: Any) -> None:
"""
Create tables with names derived from the classes that originated them.
"""
super().__init_subclass__(**kwargs) # pyright: ignore[reportUnknownMemberType]
cls.__tablename__ = camelToSnake(cls.__name__)
@classmethod
def _get_or_create_own(
cls: type[T],
session: Session,
defaults: Dict[str, Any] | None = None,
**kwargs: Any,
) -> tuple[T, bool]:
"""
Get or create an instance of the model.
Returns a tuple of (instance, created), where `created` is a boolean indicating whether the instance was created.
"""
instance = session.query(cls).filter_by(**kwargs).first()
if instance:
return instance, False
try:
instance = cls(**kwargs, **(defaults or {}))
session.add(instance)
session.commit()
return instance, True
except IntegrityError:
session.rollback()
instance = session.query(cls).filter_by(**kwargs).first()
if not instance:
raise
return instance, False
@classmethod
def get_or_create(
cls: type[T],
session: Session,
data: Dict[str, Any],
defaults: Dict[str, Any] | None = None,
) -> tuple[T, bool]:
"""
Recursively get or create an instance of the model and its nested relationships.
Returns a tuple of (instance, created), where `created` is a boolean indicating whether the instance was created.
"""
# Separate relationship data from direct attributes
attributes: Dict[str, Any] = {}
relationships: Dict[str, Dict[str, Any]] = {}
for key, value in data.items():
if isinstance(value, dict): # True if this is a relationship
relationships[key] = value
else:
attributes[key] = value
# Handle nested relationships first
for rel_name, rel_data in relationships.items():
# Get the relationship property
rel_prop: RelationshipProperty[Any] = getattr(
cls, rel_name
).property
related_cls = rel_prop.mapper.class_
# Recursively get or create the related object
related_obj, _ = related_cls.get_or_create(session, rel_data)
attributes[rel_name] = related_obj
# Now get or create the main object
return cls._get_or_create_own(session, defaults=defaults, **attributes)
from sqlalchemy import (
ForeignKey as fk,
UniqueConstraint as unique,
)
from sqlalchemy.orm import (
Mapped,
mapped_column as column,
relationship,
validates,
)
from typing import Optional
from utils.base_model import BaseModel
from utils.constants import (
CNPJ,
GENERIC_STRING as STRING,
CEP,
STATE,
AddressType,
)
from utils.functions import (
backref,
childOf,
defaultPrimaryKey as pk,
digits,
parentPrimaryKey as ppk,
)
class Region(BaseModel):
name: Mapped[STRING] = column(primary_key=True)
# Relationships
neighborhoods: Mapped[list["Neighborhood"]] = childOf("region")
adjacent_regions: Mapped[list["Region"]] = relationship(
secondary="adjacent_regions",
primaryjoin="AdjacentRegions.left_id==Region.name",
secondaryjoin="AdjacentRegions.right_id==Region.name",
back_populates="adjacent_regions",
)
class Neighborhood(BaseModel):
id: Mapped[int] = pk()
name: Mapped[STRING] = column()
city: Mapped[STRING] = column()
state: Mapped[STATE]
# Foreign keys
region_name: Mapped[STRING] = column(fk("region.name"))
# Relationships
region: Mapped["Region"] = backref("neighborhoods")
addresses: Mapped[list["Address"]] = childOf("neighborhood")
__table_args__: tuple[unique,] = (unique("name", "city", "state"),)
class Address(BaseModel):
id: Mapped[int] = pk()
street: Mapped[STRING]
number: Mapped[int | None]
complement: Mapped[STRING | None]
postal_code: Mapped[CEP] = column(digits("postal_code"))
type: Mapped[AddressType]
# Foreign keys
neighborhood_id: Mapped[int] = column(fk("neighborhood.id"))
# Relationships
neighborhood: Mapped["Neighborhood"] = backref("addresses")
facility: Mapped[Optional["Facility"]] = childOf("address")
residents: Mapped[list["Person"]] = childOf("address")
__table_args__: tuple[unique,] = (
unique("street", "number", "complement", "neighborhood_id"),
)
class Facility(BaseModel):
id: Mapped[int] = ppk("address")
name: Mapped[str]
active: Mapped[bool] = column(default=True)
# Relations
address: Mapped["Address"] = backref("facility")
employees: Mapped[list["Employee"]] = relationship(
back_populates="workplaces", secondary="workplace_employee"
)
dispensations: Mapped[list["Dispensation"]] = backref("pharmacy")
distributor: Mapped[Optional["Distributor"]] = childOf("facility")
supply_requests: Mapped[list["SupplyRequest"]] = childOf("facility")
stock: Mapped["Stock"] = childOf("facility")
shipments_sent: Mapped[list["Shipment"]] = relationship(
foreign_keys="Shipment.origin_id",
back_populates="origin",
)
shipments_received: Mapped[list["Shipment"]] = relationship(
foreign_keys="Shipment.destination_id",
back_populates="destination",
)
@validates("address")
def validate_address(self, key: Mapped["Address"], address: "Address"):
if address and address.type == AddressType.Residence:
raise ValueError(
"Facility cannot reference an address of type 'Residence'"
)
return address
class Stock(BaseModel):
medication_instance_id: Mapped[int] = ppk("medication_instance")
facility_id: Mapped[int] = column(fk("facility.id"))
# Relationships
facility: Mapped["Facility"] = backref("stock")
medication_instance: Mapped["MedicationInstance"] = backref("storage")
class Distributor(BaseModel):
id: Mapped[int] = ppk("facility")
# Foreign keys
supplier_id: Mapped[CNPJ] = column(fk("company.id"))
# Relationships
supplier: Mapped["Company"] = backref("distributors")
facility: Mapped["Facility"] = backref("distributor")
Imagine I use get_or_create with the Address class to recursively create a region, a neighborhood associated with it and then a address associated with that neighborhood. How are foreign keys being set to match the recursively created parent classes?
Great question! The key to understanding how foreign keys are set correctly in your recursive get_or_create method lies in how SQLAlchemy ORM relationships and the get_or_create method interact.
Address.get_or_create(session, data) with nested data that includes a neighborhood dictionary, which itself includes a region dictionary.get_or_create method recursively creates or fetches the related Region, then the Neighborhood linked to that Region, and finally the Address linked to that Neighborhood.Recursive creation of related objects:
In your get_or_create method, you separate attributes from relationships:
for key, value in data.items():
if isinstance(value, dict): # relationship
relationships[key] = value
else:
attributes[key] = value
Then for each relationship:
for rel_name, rel_data in relationships.items():
rel_prop = getattr(cls, rel_name).property
related_cls = rel_prop.mapper.class_
related_obj, _ = related_cls.get_or_create(session, rel_data)
attributes[rel_name] = related_obj
This means the related objects (Region, Neighborhood) are created first and assigned as attributes on the parent object (Neighborhood, Address respectively).
Assigning relationship objects sets foreign keys automatically:
When you assign a related ORM object to a relationship attribute, SQLAlchemy automatically sets the corresponding foreign key columns for you.
For example, in the Address model:
neighborhood: Mapped["Neighborhood"] = backref("addresses")
neighborhood_id: Mapped[STRING] = column(fk("neighborhood.id"))
When you do:
address_instance.neighborhood = neighborhood_instance
SQLAlchemy sets address_instance.neighborhood_id = neighborhood_instance.id behind the scenes.
This works recursively:
Neighborhood.get_or_create is called with a region dict, it creates or fetches the instance.Regionneighborhood_instance.region = region_instance.neighborhood_instance.region_name = region_instance.name.Address.get_or_create is called with a neighborhood dict, it creates or fetches the Neighborhood instance.address_instance.neighborhood = neighborhood_instance.address_instance.neighborhood_id = neighborhood_instance.id.Finally, when you call _get_or_create_own with all attributes including the related objects, the foreign keys are already set correctly.
get_or_create builds the related objects bottom-up._get_or_create_own calls session.commit() after adding the new instance, so the primary keys are generated and foreign keys are valid.session.flush() to assign primary keys without committing.Foreign keys are set automatically by SQLAlchemy when you assign related ORM objects to relationship attributes during your recursive get_or_create calls. This ensures the parent-child links are correctly established in the database.