Tortoise error for creating entity with created_at & updated_at

51 views Asked by At

I'm creating a simple fastapi & tortoise python app where I have the following:

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)


and here is my simple fastapi to create company

@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}

initally I got 422: unprocessed entity, basically saying in my request I needed to include created_at and updated_at, but I thought bc they were using auto_now, I assumed that thery would be autofilled. Nevertheless, I updated the pydantic model to exclude them, which fixed the issue.

Now however, I am getting a torotise error saying created_at isn't a column in companies

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

This is very simple I assume, anyone know what I'm missing?

1

There are 1 answers

0
midhuntp On

Seems model got updated after initial migration, try to migrate the model again

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