使用 SQLAlchemy,我发现有时我错误地键入了映射到列的属性的名称,这导致很难捕获错误:
class Thing(Base):
foo = Column(String)
thing = Thing()
thing.bar = "Hello" # a typo, I actually meant thing.foo
assert thing.bar == "Hello" # works here, as thing.bar is a transient attribute created by the assignment above
session.add(thing)
session.commit() # thing.bar is not saved in the database, obviously
...
# much later
thing = session.query(Thing)...one()
assert thing.foo == "Hello" # fails
assert thing.bar == "Hello" # fails, there's no even such attribute
有没有办法配置映射类,以便分配给未映射到 SQLAlchemy 列的任何内容会引发异常?