#πŸ”’ SQLalchemy mapped column model with different name (alias)

8 messages Β· Page 1 of 1 (latest)

pastel hazel
#

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

sick glenBOT
#

@pastel hazel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pastel hazel
#

sorry i forgot to add the err message

{
  "detail": "Error: 'TorqueDrag' object has no attribute 'datetime'"
}
#

do i have to rename my column ?

#

sorry i mean the column name on my database to something like date_time

pastel hazel
#

nevermind
i can access the value with something ilke
tnd_q.scalar_one_or_none().__dict__['datetime_col']

sick glenBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.