Skip to content

How to create a Model with a filed not int table. #1352

Answered by YuriiMotov
zhiguoxu asked this question in Questions
Discussion options

You must be logged in to vote

https://fastapi.tiangolo.com/tutorial/sql-databases/#create-multiple-models

Create one more model (use inheritance to reduce duplication):

class UserBase(SQLModel):
    id: int | None = Field(default=None, primary_key=True)
    username: str
    password: str

class User(UserBase, table=True):
    pass

class UserPublic(UserBase):
    token: Optional[str] = Field(default=None)

Then use UserPublic to validate and serialize data, and use User to store\query data:

from typing import Optional
from sqlmodel import SQLModel, Field, Session, create_engine


class UserBase(SQLModel):
    id: int | None = Field(default=None, primary_key=True)
    username: str
    password: str

class User(UserBase, 

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

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