【发布时间】:2020-01-02 19:34:00
【问题描述】:
我已经知道对于一般情况:这个问题就足够了:SQLAlchemy set default value of one column to that of another column
但是当我的列依赖于 Id 列时它不起作用,因为 id 是自动生成的。
例如:使用与其他问题相同的示例
class Substance(Base):
__tablename__ = "substances"
id = Column(Integer, primary_key=True)
code = Column(String, unique=True)
name = Column(String, unique=True)
subtance_hash = Column(String, unique=True, default=hashgen)
def hashgen(context):
uid = context.get_current_parameters()['id'] + 100
name = context.get_current_parameters()['name']
code = context.get_current_parameters()['code']
return some_hash_function(uid, name, code)
def some_hash_function(uid, name, code):
# this is a stupid example for completeness
return str(uid) + str(name) + str(code)
这里的问题是 uid 是 None 因为它会在我认为提交查询时自动生成?
所以它抛出一个错误:
sqlalchemy.exc.StatementError: (builtins.KeyError) 'id'
甚至有可能做到这一点吗?如果有怎么办?
【问题讨论】:
标签: python sqlalchemy