im trying to mapped column, but with different name and im stuck so far
the model
from datetime import datetime
from app.core.database import Base
from sqlalchemy.orm import Mapped, mapped_column, class_mapper
from sqlalchemy import Column, DateTime, Double, SmallInteger, String, func
class TorqueDrag(Base):
__tablename__ = "torques_drags"
id: Mapped[int] = mapped_column("id", autoincrement=True, unique=True, nullable=False, primary_key=True, init=False)
name: Mapped[str] = mapped_column(String(20), nullable=False)
size: Mapped[float] = mapped_column(Double, nullable=False)
depth: Mapped[float] = mapped_column(Double, nullable=False)
load: Mapped[float] = mapped_column(Double, nullable=False)
torque: Mapped[float] = mapped_column(Double, nullable=True)
average: Mapped[float] = mapped_column(Double, nullable=False)
activity: Mapped[str] = mapped_column(String(50), nullable=True)
drag: Mapped[float] = mapped_column(Double, nullable=True)
datetime_col: Mapped[datetime] = mapped_column("datetime", DateTime, nullable=False)
def as_dict(self):
result = {column.name: getattr(self, column.name) for column in class_mapper(self.__class__).columns}
if 'datetime_col' in result and isinstance(result['datetime_col'], datetime):
result['datetime_col'] = result['datetime_col'].isoformat()
return result
query
if last_tnd is not None:
tnd_activity = self.determine_tnd_activity(last_tnd, tnd)
tnd_q = await db.execute(
update(TorqueDrag)
.where(TorqueDrag.id == last_tnd.id)
.values(torque=tnd_activity.torque, drag=tnd_activity.drag)
.returning(TorqueDrag)
)
print(tnd_q.scalar_one_or_none().as_dict())
the reason im rename my datetime into datetime_col because there's python datetime default type, so i rename datetime into datetime_col, but on my database the column name is still datetime