SQLAlchemy将一列的默认值设置为另一列的默认值

编程入门 行业动态 更新时间:2024-10-08 10:50:16
本文介绍了SQLAlchemy将一列的默认值设置为另一列的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为一个物质编写一个类,该物质的名称已归档(该名称为实验室中常用的名称),另一列为长名称(以防实际名称不完整)。如果没有指定长名称,是否有某些方法告诉类仅将名称字段的值复制到长名称字段?

I am trying to write a class for substances which has a name filed (for the name, as commonly used in the lab) and another column for the long name (in case the name is actually incomplete). Is there some wy to tell the class to just copy the value of the name field to the long name field in case a long name is not specified?

我尝试了类似

class Substance(Base): __tablename__ = "substances" id = Column(Integer, primary_key=True) code = Column(String, unique=True) name = Column(String, unique=True) long_name = Column(String, unique=True, default=name)

但这失败了,因为 name 是未定义。我还有什么可以做的?

But this fails, since name is undefined. Is there anything else I could do?

推荐答案

您可以创建上下文敏感的默认函数

def mydefault(context): return context.get_current_parameters()['name'] class Substance(Base): __tablename__ = "substances" id = Column(Integer, primary_key=True) code = Column(String, unique=True) name = Column(String, unique=True) long_name = Column(String, unique=True, default=mydefault)

更多推荐

SQLAlchemy将一列的默认值设置为另一列的默认值

本文发布于:2023-10-16 13:58:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:默认值   设置为   SQLAlchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!