我将 sqlalchemy 与金字塔框架一起使用,并且我想使用他的邮政编码将一个人链接到他的地理部门。所以我在定义department_id 列定义department_id 时尝试使用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:未定义名称“dep_id_from_postcode”
我在这里找到了关于 python 执行函数的文档:http: //docs.sqlalchemy.org/en/latest/core/schema.html ?highlight=trigger#python-executed-functions 但没有在 onupdate 中使用另一个字段争论。
我希望我很清楚,因为我不是“天生的英语演讲者”谢谢大家