使用created_at和updated_at创建实体时出现Tortoise错误

问题描述 投票:0回答:1

我正在创建一个简单的 fastapi 和 tortoise python 应用程序,其中包含以下内容:

class Companies(Model):
    id = fields.UUIDField(pk=True)
    name = fields.CharField(max_length=20)
    location = fields.CharField(max_length=100)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

companies_pydentic= pydantic_model_creator(Companies, name="Companies")
companies_pydenticIn= pydantic_model_creator(Companies, name="CompaniesIn", exclude_readonly=True)


这是我创建公司的简单 fastapi

@router.post('/')
async def create_companies(company_info: companies_pydenticIn):
    print(company_info)
    company_obj = await Companies.create(**company_info.dict(exclude_unset=True))
    print(company_obj)
    response = await companies_pydentic.from_tortoise_orm(company_obj)
    return {"status": "ok", "data" : response}

最初我得到了 422:未处理的实体,基本上是说在我的请求中我需要包含created_at和updated_at,但我认为bc他们正在使用auto_now,我假设它们会被自动填充。尽管如此,我更新了 pydantic 模型以排除它们,这解决了问题。

但是现在,我收到一个 torotise 错误,指出created_at 不是公司中的列

tortoise.exceptions.OperationalError: column "created_at" of relation "companies" does not exist

我想这很简单,有人知道我错过了什么吗?

python fastapi tortoisesvn tortoise-orm
1个回答
0
投票

似乎模型在初始迁移后已更新,请尝试再次迁移模型

https://tortoise.github.io/migration.html

© www.soinside.com 2019 - 2024. All rights reserved.