user13150971
user13150971

Reputation:

List with PonyORM and FastAPI

I am trying to save a class with a list as an attribute in a db managed by PonyORM but apparently PonyORM does not have support for lists so I am trying to do it with a StrArray but it can't. Does anyone have any better ideas how to do this? The idea is that the Game class has a chat attribute which was intended to model it with a list of strings.

ENDPOINT:

@app.post("/game/", response_model=Game, status_code=201)
def create_game(game: Game):
    with db_session:
        if db.Game.exists(gameName=game.gameName):
            raise HTTPException(status_code=400, detail="Duplicate")
        return db.Game(**game.dict()).to_dict()

MODEL:

    class Game(BaseModel):
        gameName: str
        password: str
        numPlayers: int
        players: Set[Player]
        gameStatus: GameStatus
        chat: List[str]

ENTITY:

class Game(db.Entity):
    id = PrimaryKey(int, auto=True)
    gameName = Required(str, unique=True)
    password = Required(str)
    numPlayers = Required(int)
    players = Set(Player)
    gameStatus = Required(GameStatus)
    chat = Required(StrArray)

ERROR:
  File "/home/nibblex/Descargas/SV/venv/lib/python3.7/site-packages/pydantic/schema.py", line 646, in add_field_type_to_schema
    if issubclass(field_type, type_):
TypeError: issubclass() arg 1 must be a class

Upvotes: 0

Views: 605

Answers (0)

Related Questions