Alex
Alex

Reputation: 189

Many to many query in sqlalchemy

There are tables for my question.

class TemplateExtra(ExtraBase, InsertMixin, TimestampMixin):
    __tablename__ = 'template_extra'

    id = Column(Integer, primary_key=True, autoincrement=False)
    name = Column(Text, nullable=False)
    roles = relationship(
        'RecipientRoleExtra',
        secondary='template_to_role',
    )


class RecipientRoleExtra(
    ExtraBase, InsertMixin, TimestampMixin,
    SelectMixin, UpdateMixin,
):
    __tablename__ = 'recipient_role'

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Text, nullable=False)
    description = Column(Text, nullable=False)


class TemplateToRecipientRoleExtra(ExtraBase, InsertMixin, TimestampMixin):
    __tablename__ = 'template_to_role'

    id = Column(Integer, primary_key=True, autoincrement=True)
    template_id = Column(Integer, ForeignKey('template_extra.id'))
    role_id = Column(Integer, ForeignKey('recipient_role.id'))

I want to select all templates with prefetched roles in two sql-queries like Django ORM does with prefetch_related. Can I do it? This is my current attempt.

def test_custom():
    # creating engine with echo=True
    s = DBSession()

    for t in s.query(TemplateExtra).join(RecipientRoleExtra, TemplateExtra.roles).all():
        print(f'id = {t.id}')
        for r in t.roles:
            print(f'-- {r.name}')

But..

  1. it generates select query for every template to select its roles. Can I make sqlalchemy to do only one query?
  2. generated queries for roles are without join, just FROM recipient_role, template_to_role with WHERE %(param_1)s = template_to_role.template_id AND recipient_role.id = template_to_role.role_id. Is it correct?

Can u help me?

Upvotes: 1

Views: 164

Answers (1)

ky_aaaa
ky_aaaa

Reputation: 320

Based on this answer: flask many to many join as done by prefetch_related from django

Maybe somthing like this:

roles = TemplateExtra.query.options(db.joinedload(TemplateExtra.roles)).all

Let me know if it worked.

Upvotes: 1

Related Questions