2012-06-21 54 views
2

我使用sqlalchemy與金字塔框架,並且我想使用他的郵政編碼將一個人鏈接到他的地理部門。 所以我嘗試在定義department_id列時定義department_id時使用onupdate參數。 看到休耕代碼:基於sqlalchemy聲明基礎的另一個字段onupdate

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]) 

上更新了的updated_at領域工作正常,但對於deparment_id領域它告訴我:

NameError: name 'dep_id_from_postcode' is not defined

我已經找到關於Python的文檔在此執行功能:http://docs.sqlalchemy.org/en/latest/core/schema.html?highlight=trigger#python-executed-functions 但沒有使用其他字段在onupdate參數中使用的內容。

我希望我清楚到足夠多的,因爲我不是一個「自然英語的人」 謝謝大家

回答

5

的SQLAlchemy對違約使用其他列的值(即的onupdate)函數調用的特殊機制:上下文相關的默認功能 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.

廂式指出,需要確保郵政編碼是爲Person定義的字段,或者您需要添加功能,這些功能將關注獲取與Person實例關聯的郵編。

對我而言有效 - 常規功能,不受任何物體約束。 SQLAlchemy將在插入和/或更新時調用它,並通過「context」的特殊參數 - 這不是您正在更新的實際對象。

所以對於你的例子,我會做這樣的事情。

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) 
    # ... 

小心這方面的說法 - 我結束了問題,當背景有在某些情況下無字段的值,如果我沒有更新「郵政編碼」值具有相同的操作。

Eclipse與pydev調試器幫助我看到什麼信息作爲上下文傳遞。

+0

也謝謝你,它正是我正在做什麼後readeing範的回答。東西是context.current_parameters似乎是隨機填充的(有時它包含一個關鍵的郵政編碼,有時不是)(它似乎並不連接,無論我在哪裏或不更新郵編字段)(也許因爲我是一個20其他的東西該類別中的夥伴) – Yohann

+0

僅供參考,更新的鏈接:http://docs.sqlalchemy.org/en/rel_0_9/core/defaults。html#context-sensitive-default-functions –

+2

當你調用'.update'來更新一些值並且它們不包含''postcode''時,這將不起作用。 – fiatjaf

7

移動其使用前函數定義:

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

是在postcode真的字段直接在Person?因爲如果不是這樣,你可能需要完全不同的處理方式。例如,如果postcode源自primary_address關係,則需要檢查添加/刪除primary_address關係以及相關對象的更改以確保正確掛鉤。

+0

我們幾乎在那裏謝謝你的麪包車! – Yohann

+0

我檢查這個答案,因爲麪包車自己的bonty,因爲這是誰幫我解決問題的帖子,另請參見第二和我的第三個職位,有一個完整的答案 – Yohann

相關問題