Suppose I have an object model A with a one-to-many relationship with B in Peewee using the sqlite backend. I want to get some set from A and join them with their last B. Is this a way to do this without a loop?
class A(Model): some_field = CharField() class B(Model): a = ForeignKeyField(A) date = DateTimeField(default=datetime.datetime.now)
A naive way would be to call order_by and limit (1), but this applies to the whole request, so
q = A.select().join(B).order_by(B.date.desc()).limit(1)
naturally produce a singleton result like
q = B.select().order_by(B.date.desc()).limit(1).join(A)
I either use prefetch incorrectly or it doesnβt work for this because
q1 = A.select() q2 = B.select().order_by(B.date.desc()).limit(1) q3 = prefetch(q1,q2) len(q3[0].a_set) len(q3[0].a_set_prefetch)
None of these sets have a length of 1, if required. Does anyone know how to do this?
source share