-
Hi everyone want to know if it is possible to get all data from 2 many to many. If I have: class Parent(db.Model):
__tablename__ = 'parents'
id = db.Column(db.Integer, primary_key=True)
def __init__(self, **kw):
super().__init__(**kw)
self._children_1 = set()
self._children_2 = set()
@property
def children_1(self):
return self._children_1
def add_child_1(self, child):
self._children_1.add(child)
child._parents.add(self)
@property
def children_2(self):
return self._children_2
def add_child_2(self, child):
self._children_2.add(child)
child._parents.add(self)
class ChildOne(db.Model):
__tablename__ = 'children'
id = db.Column(db.Integer, primary_key=True)
def __init__(self, **kw):
super().__init__(**kw)
self._parents = set()
@property
def parents(self):
return self._parents
class ChildTwo(db.Model):
__tablename__ = 'children2'
id = db.Column(db.Integer, primary_key=True)
def __init__(self, **kw):
super().__init__(**kw)
self._parents = set()
@property
def parents(self):
return self._parents
class ParentXChildOne(db.Model):
__tablename__ = 'parents_x_children'
parent_id = db.Column(db.Integer, db.ForeignKey('parents.id'))
child_id = db.Column(db.Integer, db.ForeignKey('children.id'))
class ParentXChildTwo(db.Model):
__tablename__ = 'parents_x_children'
parent_id = db.Column(db.Integer, db.ForeignKey('parents.id'))
child_id = db.Column(db.Integer, db.ForeignKey('children2.id')) What will be the query to get both children and data from parent at the same time ? |
Beta Was this translation helpful? Give feedback.
Answered by
OmniCed
Aug 16, 2021
Replies: 1 comment
-
If someone need the answer : query = (
Parent.outerjoin(ParentXChildOne)
.outerjoin(ParentXChildTwo)
.outerjoin(ChildTwo)
.outerjoin(ChildOne)
.select()
.where(Parent.id == model_id)
)
loader = Parent.distinct(Parent.id).load(
add_child_1=ChildOne.distinct(ChildOne.id),
add_child_2=ChildTwo.distinct(ChildTwo.id)
)
db_response = await query.gino.load(loader).all() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
OmniCed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If someone need the answer :