Skip to content

Dynamically set sql-default value with table-name in SQLModel #569

Closed Answered by matthiasburger
matthiasburger asked this question in Questions
Discussion options

You must be logged in to vote

too bad nobody is answering... found a solution now:

class MyTable(SQLModel):
    sequence_id: str = Field(alias="sequence_id")
  
    @declared_attr
    def sequence_id(cls):
        return Column(
            'sequence_id',
            VARCHAR(50),
            server_default=text(f"SELECT '{cls.__tablename__}_' + convert(varchar(10), NEXT VALUE FOR dbo.sequence)"))

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by matthiasburger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant