tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.37k stars 355 forks source link

OperationalError while bulk updating the tortoise model. #1633

Closed adhilabu closed 4 weeks ago

adhilabu commented 1 month ago

Facing OperationalError while bulk updating the product model with the below data. Here I have set basemodel_ptr_id as the primary key in the database and I have assinged basemodel_ptr_id to id in the product model.

Product Model

class ProductModel(Model):
    basemodel_ptr: ForeignKeyFieldInstance[BaseModel]  = fields.OneToOneField('models.BaseModel') 
    id = fields.IntField(unique=True, pk=True, source_field="basemodel_ptr_id")
    name = fields.CharField(max_length=200)
    no_of_items = fields.DecimalField(max_digits=20, decimal_places=2)

Error

tortoise.exceptions.OperationalError: column "id" does not exist

To Reproduce

This below line of code will replicate the issue with the above product model.

ProductModel.bulk_update(objects=prods_to_update, fields=['no_of_items'], batch_size=50, using_db=db_conn)

Expected behavior

Latest model should be updated without any error and it should identify the primary key based on the source field defined for the model in the id.

abondar commented 4 weeks ago

Fixed in 0.21.3