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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:02:37+00:00 2026-05-29T06:02:37+00:00

Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from

  • 0

Say I have a SqlAlchemy model something like this:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
Base = declarative_base()
Session = sessionmaker()

class EmployeeType(Base):
    __tablename__ = 'employee_type'
    id = Column(Integer(), primary_key=True)
    name = Column(String(20))

class Employee(Base):
    __tablename__ = 'employee'
    id = Column(Integer(), primary_key=True)
    type_id = Column(Integer(), ForeignKey(EmployeeType.id))
    type = relationship(EmployeeType, uselist=False)

session = Session()
session.add(EmployeeType(name='drone'))
session.add(EmployeeType(name='PHB'))

I’d like to have some kind of “relationship” from Employee directly to EmployeeType.name as a convenience, so I can skip the step of looking up an id or EmployeeType object if I have a type name:

emp = Employee()
emp.type_name = "drone"
session.add(emp)
session.commit()
assert (emp.type.id == 1)

Is such a thing possible?

EDIT: I found that association_proxy can get me partway there:

class Employee(Base):
    ...
    type_name = association_proxy("type", "name")

the only problem being that if I assign to it:

emp = session.query(Employee).filter_by(EmployeeType.name=='PHB').first()
emp.type_name = 'drone'

it modifies the employee_type.name column, not the employee.type_id column.

  • 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-05-29T06:02:40+00:00Added an answer on May 29, 2026 at 6:02 am

    I agree with Jonathan’s general approach, but I feel like adding an employee object to the session and setting the employee type should be independent operations. Here’s an implementation that has type_name as a property and requires adding to the session before setting it:

    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, String, Integer, ForeignKey
    from sqlalchemy.orm import sessionmaker, relationship
    Base = declarative_base()
    Session = sessionmaker()
    
    class EmployeeType(Base):
        __tablename__ = 'employee_type'
        id = Column(Integer(), primary_key=True)
        name = Column(String(20))
    
    class Employee(Base):
        __tablename__ = 'employee'
        id = Column(Integer(), primary_key=True)
        type_id = Column(Integer(), ForeignKey(EmployeeType.id))
        type = relationship(EmployeeType)
    
        @property
        def type_name(self):
            if self.type is not None:
                return self.type.name
            return None
    
        @type_name.setter
        def type_name(self, value):
            if value is None:
                self.type = None
            else:
                session = Session.object_session(self)
                if session is None:
                    raise Exception("Can't set Employee type by name until added to session")
                self.type = session.query(EmployeeType).filter_by(name=value).one()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
let say have element like this <div class=watch-me style=display: none;>Watch Me Please</div> as we
Say have normal class in iOS, called A. I'd like to pass this as
Say I have an Intent like this: Intent intent = new Intent(context, MyActivity.class); I
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have a class named Frog, it looks like: public class Frog {
Say I have two strings, String s1 = AbBaCca; String s2 = bac; I
Say I have a ClassWithManyDependencies. I want to write a Guice Provider for this
say I have input data like so: firstName | lastName | Country Bob |

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.