Skip to content

How to create a basemodel with relationship? #858

Discussion options

You must be logged in to vote

I think you are just missing the parent class specification: class EditableBase(SQLModel)

But from experience it's not a good idea to define relationships in base class, it caused me so many problems.

Now I just put the type and default value for relationships in Base and specify the relationship in Table class. That is where it will actually be used anyway.

Example:

class EditableBase(SQLModel):
    create_time: datetime = Field(sa_column_kwargs=dict(server_default=func.now()))
    update_time: datetime = Field(sa_column_kwargs=dict(server_default=func.now(), onupdate=func.now()))

    creator: Optional["User"] = None
    updater: Optional["User"] = None

class EditableTable(EditableBase…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@PaleNeutron
Comment options

Answer selected by PaleNeutron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants