Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy loading of columns in sqlalchemy

Is it possible in sqlalchemy to lazy load a column? I have large BLOB column that I'm usually using but in some cases I don't want to load this specific column (while I'd like to have ORM object). Is it possible without adding separate table?

like image 910
Maciej Piechotka Avatar asked Jun 03 '11 10:06

Maciej Piechotka


People also ask

What is SQLAlchemy lazy loading?

Lazy loading refers to objects are returned from a query without the related objects loaded at first. When the given collection or reference is first accessed on a particular object, an additional SELECT statement is emitted such that the requested collection is loaded.

How do I get column names in SQLAlchemy?

To access the column names we can use the method keys() on the result. It returns a list of column names. Since, we queried only three columns, we can view the same columns on the output as well.

What is Backref in SQLAlchemy?

The sqlalchemy backref is one of the type keywords and it passed as the separate argument parameters which has to be used in the ORM mapping objects. It mainly includes the event listener on the configuration attributes with both directions of the user datas through explicitly handling the database relationships.

What is With_entities?

with_entities() can either add or remove (simply: replace) models or columns; you can even use it to modify the query, to replace selected entities with your own function like func.count() : query = User.query count_query = query.with_entities(func.count(User.


1 Answers

class Book(Base):
    __tablename__ = 'books'

    book_id = Column(Integer, primary_key=True)
    title = Column(String(200), nullable=False)
    summary = Column(String(2000))
    excerpt = deferred(Column(Text))
    photo = deferred(Column(Binary))

Deferred Column Loading

like image 107
ba__friend Avatar answered Sep 26 '22 01:09

ba__friend