pyodmongo icon indicating copy to clipboard operation
pyodmongo copied to clipboard

Request: Allow non-standard IDs

Open manuelseeger opened this issue 1 year ago • 1 comments

Description

I have collections with IDs that are not BSON ObjectIds, but strings of a different length.

So far this has worked with pyodm - except for inserts. Actually the insert itself also works in that everything is saved as expected. But the response from save() tries so force the returned ID into ObjectID and throws ValidationError: https://github.com/mauro-andre/pyodmongo/blob/3e23c64dba8e69de2cc4ef0ac737c13580eda27f/pyodmongo/engine/engine.py#L42

So, feature request: save_response should respect the type of the ID from the supplied model, instead of forcing ObjectId

Ideally, this would also be the case when calling save() without a query. I got UPSERT to work with

db.save(replay, query=eq(Product.id, product.id))

where id is a non-ObjectId identifier. Ideally I could also chose to not supply a query here and save() would respect the type of the id field of the model.

manuelseeger avatar Mar 02 '24 16:03 manuelseeger

@manuelseeger, Pyodmongo was initially designed with the assumption that the _id field would always be an ObjectId. However, I believe I can find a way to make this change without causing significant refactoring in the code.

mauro-andre avatar Mar 12 '24 08:03 mauro-andre