Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:38:54+00:00 2026-06-17T19:38:54+00:00

Given this Many to Many relationship: tagmap = db.Table(‘tagmap’, db.Model.metadata, db.Column(‘post_id’, db.Integer, db.ForeignKey(‘posts.id’), db.Column(‘tag_id’,

  • 0

Given this Many to Many relationship:

tagmap = db.Table('tagmap', db.Model.metadata,
    db.Column('post_id', db.Integer, db.ForeignKey('posts.id'),
    db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'),)

class Post(db.Model):
    __tablename__ = 'posts'
    id    = db.Column(db.Integer, primary_key=True)
    text  = db.Column(db.Text)
    tags  = db.relationship('Tag', secondary=tagmap, backref='posts')

class Tag(db.Model):
    __tablename__ = 'tags'
    id    = db.Column(db.Integer, primary_key=True)
    name  = db.Column(db.String)

How would you construct a query to select all the Posts including an iterable list of associated Tag objects?

Desired query result structure:

[ <Post 1, "Blah",      [<Tag "Goober">, <Tag "Mexico">]>,
  <Post 2, "Daft Punk", [<Tag "French">]>,
  <Post 3, "Slurpee",   [<Tag "Diabetes">, <Tag "Tasty">, <Tag "Sugary">]>,
  <Post 4, "Lasers",    []> ]

Example desired result usage in a Jinja template:

{% for post in posts %}
    {{ post.text }}
    <hr>
    {% for tag in post.tags %}
        {{ tag.name }}, 
    {% endfor %}
{% endfor %}

Edit:

When trying to extend the query, the tags stop being included. See failed query below:

Post.query.join(User)\
    .options(db.joinedload(Post.tags))\
    .order_by(Post.create_date.desc()).limit(5)\
    .values(User.name.label('author'),
            Post.create_date,
            Post.text,)

Edit 2:

So this query works great:

posts = db.session.query(Post, User.name.label('author')).join(User)\
            .options(db.joinedload(Post.tags))\
            .order_by(Post.created.desc()).limit(5)

but if I want to be selective about the columns in Post I select:

# I only need the post text, nothing else
posts = db.session.query(Post.text, User.name.label('author')).join(User)\
            .options(db.joinedload(Post.tags))\
            .order_by(Post.created.desc()).limit(5)

It begins to fall apart quickly:

ArgumentError: Query has only expression-based entities - can't find property named 'tags'.

So I try to add the tags back:

# Yes, I know this is wrong
posts = db.session.query(Post.text, Post.tags, User.name.label('author'))\
            .join(User)\               
            .options(db.joinedload(Post.tags))\
            .order_by(Post.created.desc()).limit(5)

Which still fails.

So if you only want specific columns from the parent object (Post), where to you say in the expression, “I still want the tags”?

It’s not really that important now, but I figured it would be useful to know. Or even know that it’s not possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:38:56+00:00Added an answer on June 17, 2026 at 7:38 pm

    I assume that you want to use eager loading for relationships. You can achieve that either by setting lazy keyword argument in your relationship to 'joined' or 'subquery':

    class Post(db.Model):
        # ...
        tags = db.relationship('Tag', secondary=tagmap, backref='posts',
                               lazy='joined')    # Or lazy='subquery'
    

    Or you can set it per query:

    q = Post.query.options(db.joinedload(Post.tags)).all()
    # Or db.subqueryload(Post.tags)
    

    Refer to Eager Loading part of ORM tutorial and Relationship Loading Techniques for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
This is situation I have: I have two entities with one-to-many relationship mapped like
I have the same tables as in this question: MySql query over many-to-many relationship
Given this relationship: class Doc < ActiveRecord::Base has_and_belongs_to_many :articles end and class Article <
Is this a relationship that can be described in Ruby on Rails' ActiveRecord model
We have two tables, ActivityForm and Field which are given a many-to-many relationship via
I have a model like this: Client - which has many Locations - which
My application has an Entity Framework model containing a many-to-many relationship like the following:
Given a classic example of Student and Subject where they have a many-to-many relationship,
My domain contains a one-to-many relationship like this: class A { B b }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.