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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:19:32+00:00 2026-06-06T07:19:32+00:00

I use sqlalchemy with the pyramid framework, and i want to link a person

  • 0

I use sqlalchemy with the pyramid framework, and i want to link a person to his geographical department using his postcode.
So i try to use the onupdate argument when defining the department_id column define the department_id.
see fallowing code:

from datetime import date
from emailing.models import Base, DBSession
from sqlalchemy import Column, Integer, Unicode, Text, DateTime, Sequence, Boolean, Date, UnicodeText, UniqueConstraint, Table, ForeignKey
from sqlalchemy.orm import scoped_session, sessionmaker, column_property, relationship, backref
from sqlalchemy.sql import func

class Person(Base):
    __tablename__ = u'person'
    id = Column(Integer, primary_key=True)

    firstName = Column(Unicode(255))
    lastName = Column(Unicode(255))

    created_at = Column(Date, default=func.now())
    updated_at = Column(Date, onupdate=func.now())

    department_id = Column(Integer(), ForeignKey('department.id'), onupdate=dep_id_from_postcode)
    department = relationship("Department", backref='persons')


    __table_args__ = (UniqueConstraint('firstName', 'lastName'), {})


    def dep_id_from_postcode(self):
        return int(self.postcode[:2]) 

on update for the updated_at field works fine, but for the deparment_id field it tell my:

NameError: name ‘dep_id_from_postcode’ is not defined

i’ve found documentation about python executed function here: http://docs.sqlalchemy.org/en/latest/core/schema.html?highlight=trigger#python-executed-functions
but nothing that uses another field to use in onupdate argument.

i hope i’m clear enought as i’m not a “natural english speaker”
Thank you all

  • 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-06T07:19:35+00:00Added an answer on June 6, 2026 at 7:19 am

    SQLAlchemy has special mechanism for using other column value in default (i.e. onupdate) function called: Context-Sensitive Default Functions
    http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#context-sensitive-default-functions:

    The typical use case for this context with regards to default
    generation is to have access to the other values being inserted or
    updated on the row.

    As Van pointed out you need to make sure that postcode is a field defined for Person or you’ll need to add functionality that will take care about getting postcode associated with Person instance.

    What worked for me – regular function, not bound to any object.
    SQLAlchemy will call it at the time of insert and/or update and pass special argument with “context” – which is not actual object you are updating.

    So for your example I would do something like this.

    def dep_id_from_postcode(context):
        postcode = context.current_parameters['postcode']
        return int(postcode[:2])
    
    class Person(Base):
        postcode = Column(String)
        # ...
    
        # ...
        department_id = Column(Integer(), ForeignKey('department.id'), onupdate=dep_id_from_postcode)
        # ...
    

    Be careful with this context argument – I end up with problem when context had None field’s value in some cases if I not updating ‘postcode’ value with the same operation.

    Eclipse with pydev with debugger helped me to see what information is passed as context.

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

Sidebar

Related Questions

I am trying to use pyramid beaker in Pyramid framework and its just not
I am using a relational database via SQLAlchemy. I want to spawn a job
I am using SQlAlchemy in my web project. What should I use - scoped_session(sessionmaker())
I create a table in database using sqlalchemy and now want to make a
I'm building an app on Pyramid framework and would like to use the web2py-DAL
I'm creating the web app using Pyramid-1.2.1 with SQLAlchemy as database backend. Now I
Why do people use SQLAlchemy instead of MySQLdb? What advantages does it offer?
SQLAlchemy seems really heavyweight if all I use is MySQL. Why are convincing reasons
I am working with SQLAlchemy, and I'm not yet sure which database I'll use
Use case: user clicks the link on a webpage - boom! load of files

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.