sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

select specific columns returning Row objects instead of ORM class, load_only() doesn't work

Open johnatr opened this issue 3 years ago • 2 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the SQLModel documentation, with the integrated search.
  • [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to SQLModel but to Pydantic.
  • [X] I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

1)

from sqlalchemy.orm import load_only
from sqlmodel import (Field, Session, SQLModel, create_engine, select)
from chalicelib.models import ( Tx )

engine = create_engine(url, echo=True)

        with Session(engine) as session:
            results = session.exec(select(Tx.Id, Tx.party, Tx.time)
                                   .order_by(Tx
                                             .time
                                             .desc())
                                   ).all()

            myResults = []
            
            for result in results:
                myResults.append(result.toDict())

2)

from sqlalchemy.orm import load_only
from sqlmodel import (Field, Session, SQLModel, create_engine, select)
from chalicelib.models import ( Tx )

engine = create_engine(url, echo=True)

        with Session(engine) as session:
            results = session.exec(select(Tx)
                                   .options(load_only("id", "party", "time")))
                                   .order_by(Tx
                                             .time
                                             .desc())
                                   ).all()
            myResults = []
            
            for result in results:
                myResults.append(result.toDict())

Description

Basically I'm following the two methods mentioned here: https://github.com/tiangolo/sqlmodel/issues/232

  1. Selecting columns inside select() returns Row class object instead of the desired ORM class Tx like before

I expected Tx(id=2, party='new', time='0000:00:00') instead got Row object that I am having trouble now converting to a dict with .toDict() that normally converts that Tx object into a dict.

  1. Using sqlalchemy's load_only filter gets ignored.

I expected Tx(id=2, party='new', time='0000:00:00') but instead got all the columns associated.

Want to know if I'm doing this the "right" way, seeing as there is no documentation and I could only find this information after coming across #232 via Google search.

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.8

Additional Context

No response

johnatr avatar May 11 '22 20:05 johnatr

  1. Using sqlalchemy's load_only filter gets ignored.

The excluded columns will still be retrieved if you inspect them within the context of the session. Could it be that this lazy loading gives you the impression that load_only does not work?

byrman avatar May 18 '22 21:05 byrman