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 8704015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:59:37+00:00 2026-06-13T02:59:37+00:00

class Geolocation(db.Model): __tablename__ = geolocation id = db.Column(db.Integer, primary_key=True) latitude = db.Column(db.Float) longitude =

  • 0
class Geolocation(db.Model):
    __tablename__ = "geolocation"
    id = db.Column(db.Integer, primary_key=True)
    latitude = db.Column(db.Float)
    longitude = db.Column(db.Float)
    elevation = db.Column(db.Float)         # Meters
    # Relationships
    pin = db.relationship('Pin', uselist=False, backref="geolocation")

    def __init__(self, latitude, longitude, elevation):
        self.latitude = latitude
        self.longitude = longitude
        self.elevation = elevation

    def __repr__(self):
        return '<Geolocation %s, %s>' % (self.latitude, self.longitude)


class Pin(db.Model):
    __tablename__ = "pin"
    id = db.Column(db.Integer, primary_key=True)
    geolocation_id = db.Column(db.Integer, db.ForeignKey('geolocation.id'))  # True one to one relationship (Implicit child)

    def __init__(self, geolocation_id):
        self.geolocation_id = geolocation_id

    def __repr__(self):
        return '<Pin Object %s>' % id(self)      # Instance id merely useful to differentiate instances.


class User(Pin):
    #id = db.Column(db.Integer, primary_key=True)
    pin_id = db.Column(db.Integer, db.ForeignKey('pin.id'), primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password_hash = db.Column(db.String(120), nullable=False)
    salt = db.Column(db.String(120), nullable=False)
    # Relationships
    #posts = db.relationship('Post', backref=db.backref('user'), lazy='dynamic')               #One User to many Postings.

    def __init__(self, username, password_hash, salt, geolocation_id):
        super(Pin, self).__init__(self, geolocation_id)
        self.username = username
        self.password_hash = password_hash
        self.salt = salt

    def __repr__(self):
        return '<User %r>' % self.username

I’m confused about how to set up id’s and relationships with subclasses in SQLAlchemy (I happen to be using Flask-SQLAlchemy). My general design is to have the superclass Pin be a high level representation of anything that has a geolocation (i.e. a User, a Place, etc.).

There is a one to one relationship between a Pin and Geolocation object so a Geolocation does not contain the location of two Users (or a User and a Place) simultaneously for example. Now I want to subclass Pin to create the User class. A User object should have a name, password_hash, salt and I also want to be able to lookup the Geolocation of the User via userObj.geolocation. However, I later want to make a class Place which also subclasses Pin and I should be able to lookup the geolocation of a Place via placeObj.geolocation . Given a geolocation object, I should be able to use geolocationObj.pin to lookup the User/Place/etc. that the geolocation object corresponds to. The whole reason I introduced the superclass Pin was to ensure that there was a pure one to one relationship between Pin and Geolocation objects rather than having a Geolocation be associated with either a User or a Person which would require the Geolocation table to have user_id and place_id columns, one of which would always be null.

I was expecting every User to automatically have a .geolocation property, via the parent Pin class, which referred to a Geolocation but it seems like SQLAlchemy does not do this. How can I make subclassing relationships work to accomplish my goal of having User and Place and potentially other classes subclass Pin, have each of those classes have a geolocation property via Pin, and have a one to one relationship between a Pin and a Geolocation?

  • 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-13T02:59:38+00:00Added an answer on June 13, 2026 at 2:59 am

    The solution I came up with. This serves as a full example of subclassing in SQLAlchemy in the declarative style and using Join inheritance.

    class Geolocation(Base):
        __tablename__ = "geolocation"
        id = Column(Integer, primary_key=True)
        latitude = Column(Float)
        longitude = Column(Float)
        elevation = Column(Float)         # Meters
        # Relationships
        person = relationship('Pin', uselist=False, backref="geolocation")
    
        def __init__(self, latitude, longitude, elevation):
            self.latitude = latitude
            self.longitude = longitude
            self.elevation = elevation
    
        def __repr__(self):
            return '<Geolocation %s, %s>' % (self.latitude, self.longitude)
    
    
    class Pin(Base):
        __tablename__ = 'pin'
        id = Column(Integer, primary_key=True)
        geolocation_id = Column(Integer, ForeignKey('geolocation.id'), unique=True, nullable=False)  # True one to one relationship (Implicit child)
        type = Column('type', String(50))              # discriminator
        __mapper_args__ = {'polymorphic_on': type}
    
        def __init__(self, geolocation_id):
            self.geolocation_id = geolocation_id
    
    
    class User(Pin):
        __tablename__ = 'user'
        id = Column(Integer, ForeignKey('pin.id'), primary_key=True)
        __mapper_args__ = {'polymorphic_identity': 'user',
                           'inherit_condition': (id == Pin.id)}
        user_id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
        username = Column(String(80), unique=True)
        password_hash = Column(String(120))
        salt = Column(String(120))
        posts = relationship('Posting', primaryjoin="(User.user_id==Posting.user_id)", backref=backref('user'), lazy='dynamic')   #One User to many Postings.
    
        def __init__(self, username, password_hash, salt, geo_id):
            super(User, self).__init__(geo_id)
            self.username = username
            self.password_hash = password_hash
            self.salt = salt
    
        def __repr__(self):
            return '<User %s>' % (self.username)
    
    
    class Posting(Pin):
        __tablename__ = 'posting'
        id = Column(Integer, ForeignKey('pin.id'), primary_key=True)
        __mapper_args__ = {'polymorphic_identity': 'posting',
                            'inherit_condition': (id == Pin.id)}
        posting_id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
        creation_time = Column(DateTime)
        expiration_time = Column(DateTime)
        user_id = Column(Integer, ForeignKey('user.user_id'))              # One User to many Postings
    
        def __init__(self, creation_time, expiration_time, user_id, geo_id):
            super(Posting, self).__init__(geo_id)
            # For now, require creation time to be passed in. May make this default to current time.
            self.creation_time = creation_time
            self.expiration_time = expiration_time
            self.user_id = user_id
    
        def __repr__(self):
            #TODO come up with a better representation
            return '<Post %s>' % (self.creation_time)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am retrieving the nearest locations available from a given address (Longitude/Latitude) from geolocation
I have the following case: public class GeoLocation { public double Longitude { get;
this is the code i wrote to retrieve the latitude, longitude but it is
I have a browser application and i want to use the Geolocation class. The
I want to use variable iso3166TwoLetterCode's value from this class, Geolocation error with IP
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
class StartAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): print "Hello" start.add_argument('-s', '--start', action=StartAction) I
class A: pass def b(self): print('b') A.b = b a = A() At this
class X { int i; public: X() { i = 0; } void set(int

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.