is it possible to change how default table name are generated from the class name when using ORM ? #1031
-
First Check
Commit to Help
Example Codetoto DescriptionI'm starting with the ORM and I try y best to right as few things as possible and hand over complex things to the lib. I would like to create the following class: class CropMask(Base, table=True):
"""Cropmask used to filter data when loading indicators from GEE.
All the cropmasks are exported as images in GEE to accelerate computation.
"""
id: Optional[int] = Field(default=None, primary_key=True)
name: str
"technical name of the asset. Use snake case like 'esa_crop'"
asset_id: str
"the binary asset to use as a mask image"
geolocations: list["Geolocation"] = Relationship(back_populates="crop_mask")
"The geolocations that use this cropmask" and add a relation with the geolocation class as : class Geolocation(Base, table=True):
"""The geolocation as defined in MDM.
The Geolocation are aggregation of multiple administrative areas from GADM. They are used to reduce the amount of reducers to compute when downloading an indicator from GEE.
For example we don't need to download all France département when computing Wheat related indicators, only the ones where Wheat is grown.
The values of this table should be populated from MDM sources at building stage of the DB to match company description of geometries.
"""
id: Optional[int] = Field(default=None, primary_key=True)
gid_ldc: str
"The name of the geolocation"
display_name: str
"The name use by platform member to name this aggregation"
crop_mask_id: Optional[int] = Field(default=None, foreign_key="crop_mask.id")
crop_mask: Optional[CropMask] = Relationship(back_populates="geolocations")
"The cropmask used to filter the data when computing indicators" In the Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.19 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
from typing import Optional
from pydantic.alias_generators import to_snake
from sqlalchemy.orm import declared_attr
from sqlmodel import Field, SQLModel, create_engine
class SQLModelBase(SQLModel):
@declared_attr.directive # type: ignore[misc]
@classmethod
def __tablename__(cls) -> str:
return to_snake(cls.__name__)
class CropMask(SQLModelBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
engine = create_engine("sqlite:///", echo=True)
SQLModel.metadata.create_all(engine) Output:
|
Beta Was this translation helpful? Give feedback.
Output: