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

  • SEARCH
  • Home
  • 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 7718957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:17:41+00:00 2026-06-01T03:17:41+00:00

Models from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, ForeignKey from sqlalchemy import Integer

  • 0

Models

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey
from sqlalchemy import Integer
from sqlalchemy import Unicode
from sqlalchemy import TIMESTAMP
from sqlalchemy.orm import relationship

BaseModel = declarative_base()

class Base(BaseModel):
   __tablename__ = 'base'
   id = Column(Integer, primary_key=True)
   location = Column(Unicode(12), ForeignKey("locationterrain.location"), unique=True,)
   name = Column(Unicode(45))
   ownerid =  Column(Integer,ForeignKey("player.id"))
   occupierid =  Column(Integer, ForeignKey("player.id"))
   submitid =  Column(Integer,ForeignKey("player.id"))
   updateid =  Column(Integer,ForeignKey("player.id"))
   owner = relationship("Player",
         primaryjoin='Base.ownerid==Player.id',
         join_depth=3,
         lazy='joined')
   occupier= relationship("Player",
         primaryjoin='Base.occupierid==Player.id',
         join_depth=3,
         lazy='joined')
   submitter = relationship("Player",
         primaryjoin='Base.submitid== Player.id',
         join_depth=3,
         lazy='joined')
   updater= relationship("Player",
         primaryjoin='Base.updateid== Player.id',
         join_depth=3,
         lazy='joined')


class Player(BaseModel):
   __tablename__ = 'player'
   id = Column(Integer, ForeignKey("guildmember.playerid"), primary_key=True)
   name =  Column(Unicode(45))

Searching

bases = dbsession.query(Base)
bases = bases.order_by(Base.owner.name)

This doesn’t work …. I’ve searched everywhere and read the documentation.
But I just don’t see how I can sort my (Base) query on their ‘owner’ relationship’s name.

It always results in:

 AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object has an attribute 'name'

This must be easy… but I don’t see it. Also looked into Comparators, which seemed logical, but I don’t see where the query part for the ORDER BY is generated or what I should be returning since everything is generated dynamically. And making a comparator for each of my ‘player’ relationships to do a simple thing seems over complicated.

  • 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-01T03:17:43+00:00Added an answer on June 1, 2026 at 3:17 am

    SQLAlchemy wants you to think in terms of SQL. If you do a query for “Base”, that’s:

    SELECT * FROM base
    

    easy. So how, in SQL, would you select the rows from “base” and order by the “name” column in a totally different table, that is, “player”? You use a join:

    SELECT base.* FROM base JOIN player ON base.ownerid=player.id ORDER BY player.name
    

    SQLAlchemy has you use the identical thought process – you join():

    session.query(Base).join(Base.owner).order_by(Player.name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
this is my Flask-SQLAlchemy Declarative code: from sqlalchemy.ext.associationproxy import association_proxy from my_flask_project import db
Here's what I've got: from sqlalchemy import * from sqlalchemy.orm import * from web.models.card
I had a many-to-many relationship between two Django Models: from django.contrib.auth.models import User class
In my models.py: from django.db import models from core import tasks class Image(models.Model): image
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
I have a Pylons app that I'm using SqlAlchemy declarative models for. In order
Given this model: from django.db import models from django.contrib.auth.admin import User # Create your
I have a class Book : from django.db import models from users.models import User
My models.py from django.db import models from django.contrib.auth.models import User class Song(models.Model): uploader =

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.