Dynamically set sql-default value with table-name in SQLModel #569
-
First Check
Commit to Help
Example Codeclass BaseModel(SQLModel):
@declared_attr
def __tablename__(cls) -> str:
return cls.__name__
guid: Optional[UUID] = Field(default=None, primary_key=True)
class SequencedBaseModel(BaseModel):
sequence_id: str = Field(sa_column=Column(VARCHAR(50), server_default=text(f"SELECT '{TABLENAME}_' + convert(varchar(10), NEXT VALUE FOR dbo.sequence)")))
class Project(SequencedBaseModel):
... Descriptionalembic would generate a migration for a table SELECT '{TABLENAME}_' + convert(varchar(10), NEXT VALUE FOR dbo.sequence) and should insert into project-table the values Any idea on how to set the tablename dynamically? I cannot use a constructor for setting the columns because alembic is ignoring them, I cannot access the Operating SystemWindows, macOS Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.10.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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)")) |
Beta Was this translation helpful? Give feedback.
too bad nobody is answering... found a solution now: