SQLAlchemy set default value of one column to that of another column

前端 未结 2 1358
傲寒
傲寒 2020-12-10 11:40

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 actu

2条回答
  •  再見小時候
    2020-12-10 12:25

    You can create context-sensitive default function

    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)
    

提交回复
热议问题