Lazy loading
in-many, or many-to-time, if you want to get more data this part of the time, you can get all defined by a good correspondence between the relationship, this time acquired data list, but sometimes do not want to get all the data, if you want to filter the data you need to traverse the screening, too much trouble, you can start from inside the query returns a value, such as obtaining data which also add filter conditions, you need to add a parameter in the relationship: lazy = 'dynamic', later acquired by the correspondence relation defined relationship is not a list, but a AppenderQuery objects, such objects can either add new data, may be the same with the object data Query secondary filtration
Ready to work
datetime datetime Import from
from SQLAlchemy Import create_engine, the Column, Integer, String, the Float, the Text, a ForeignKey, the DateTime
from sqlalchemy.ext.declarative Import declarative_base
from sqlalchemy.orm Import sessionmaker, Relationship, backref
# database information
host = '127.0.0.1'
= Port '3306'
database = 'db_to_sqlalchemy'
username = 'the root'
password = '123456'
widget database # + type connection to the database, pymysql used herein
DB_URI = f'mysql + pymysql: // { username}: {password} @ {Host}: {Port} / {Database} '
engine = create_engine (DB_URI) # creation engine
base = declarative_base (engine) # use declarative_base create a base class
the session = sessionmaker (engine) ()
class the User (base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String(50), nullable=False)
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(50), nullable=False)
create_time = Column(DateTime, nullable=False, default=datetime.now)
uid = Column(Integer, ForeignKey('user.id'))
author = relationship('User', backref=backref('article', order_by=create_time))
def __repr__(self):
return f'title: {self.title}、create_time: {self.create_time}'
Base.metadata.drop_all() # 删除所有表
Base.metadata.create_all() # 创建表
user = User(username='aaa')
for x in range(100):
article = Article(title=f'title{x}')
article.author = user
session.add(article)
session.commit()
The default is to return the object list
Lazy loading specified attribute lazy = 'dynamic'
The target return is AppendQuery
Import look at the source code
As can be seen above, the object returned will have all the features and AppendQuery Query, i.e., a method may be used for data Query secondary filtration
For example, check data corresponding to the first user table all the data in the article data table inside id is greater than 95
There AppendQuery characteristics, that can add data
lazy supported parameters
First of all, the default is to select
Supported Parameters
select: If a relationship corresponding field is not adjusted, it will not get much on this side of the data, once this property call, get all of the corresponding data, returns a list of
immediate: whether or not to call a corresponding relationship field, it will acquire all of the corresponding data, returns the list
joined: the relationship to find the corresponding data field back, join by way of data added to the main table
subquery: embodiment subqueries