转载自:
https://www.cnblogs.com/lpxblog/p/7485469.html
https://www.cnblogs.com/lpxblog/p/7485337.html
首先看官网的关于lazy的说明:
lazy 决定了 SQLAlchemy 什么时候从数据库中加载数据:,有如下四个值:(其实还有个noload不常用)
select: (which is the default) means that SQLAlchemy will load the data as necessary in one go using a standard select statement.
joined: tells SQLAlchemy to load the relationship in the same query as the parent using a JOIN statement.
subquery: works like ‘joined’ but instead SQLAlchemy will use a subquery.
dynamic : is special and useful if you have many items. Instead of loading the items SQLAlchemy will return another query object which
you can further refine before loading the items. This is usually what you want if you expect more than a handful of items for this relationship
通俗了说,select就是访问到属性的时候,就会全部加载该属性的数据。joined则是在对关联的两个表进行join操作,从而获取到所有相关的对象。dynamic则不一样,在访问属性的时候,并没有在内存中加载数据,而是返回一个query对象, 需要执行相应方法才可以获取对象,比如.all().下面结合实例解释这几个的使用场景。