Reputation: 57
I need to know how to query by excluding password column. is it possible?
query = await db.execute(
select(User)
.filter(User.id.in_(users.user_ids)).where(User.is_deleted== False))
list_user = query.scalars().unique().all()
Upvotes: 1
Views: 1881
Reputation: 2305
On SQLAlchemy v2.0+, you can add a keyword argument named deferred
to the mapped_column
function to have the same effect as suggested in @msamsami's answer.
import Base
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column
class User(Base):
__tablename__ = "user"
username: Mapped[str] = mapped_column(String(255))
# ...
password: Mapped[str] = mapped_column(String(255), deferred=True) # only loaded on direct access
# ...
With deferred=True
, the password will only be loaded on direct access. You can check more in the documentation here.
Upvotes: 1
Reputation: 867
You can use the "Deferred Column Loading" feature. For example, if you want the password
column to be loaded only upon direct access, instead of when the entity is queried, you can define it using the deferred
function in your model:
from sqlalchemy.orm import deferred
from sqlalchemy import String, Column, ...
class User(Base):
__tablename__ = "user"
username = Column(String(200), nullable=False)
...
password = deferred(Column(String(2000)))
...
Upvotes: 5