0

我正在使用这样的模型:

class Question(BaseModel):
    id: int
    title: str = Field(..., min_length=3, max_length=50)
    answer_true: str = Field(..., min_length=3, max_length=50)
    answer_false: list
    category_id: int

并尝试questions使用以下功能:

def get(id: int):
    query = questions.select().where(id == questions.c.id)
    return database.fetch_one(query=query)

@router.get("/{id}/", response_model=Question)
def read_question(id: int = Path(..., gt=0),):
    question = get(id)
    if not question:
        raise HTTPException(status_code=404, detail="question not found")
    return question

这是已存储在数据库中的数据:

图片

但它无法正确返回list字段 ( answer_false),并且该字段的值作为字符返回:

图片

我做错了什么,我应该如何解决这个问题?

4

1 回答 1

0

这是因为我的 sqlalchemy 配置。我在表的配置中删除dimensions了问题得到解决:

questions = Table(
    "questions",
    metadata,
    Column("id", Integer, primary_key=True),
    Column("title", String(50)),
    Column("answer_true", String(50)),
    Column("answer_false", ARRAY(String)), ## adding dimension will cause the list to not work in get requests
    Column("created_date", DateTime, default=func.now(), nullable=False),
    Column("category_id", Integer, ForeignKey("categories.id")),
)
于 2020-05-22T14:47:40.703 回答