odmantic icon indicating copy to clipboard operation
odmantic copied to clipboard

Preserve document fields ordering

Open guesswh0 opened this issue 2 years ago • 1 comments

Saving document not preserving fields ordering:

class A(Model):
    b: int
    a: int
    
a = A(b=1, a=2)
engine.save(a)
> mongo --eval "db.publisher.find({})"
{
  "_id": ObjectId("636de7e8731f89ec756c538c"),
  "a": 2,
  "b": 1
},

The engine.save and engine.save_all methods are actually behaving as upsert operations. In other words, if the instance already exists it will be updated. Otherwise, the related document will be created in the database.

I guess this upsert=True operation is responsible for this behavior?

I found it very inconvinient to see how schema of your document is changed due to fields ordering ... Any plans on changing this, or im missing something ?

guesswh0 avatar Nov 11 '22 05:11 guesswh0

Thanks for the suggestion. I'm not sure this is related to upsert maybe there is a simpler way to fix that. I'll have a look.

art049 avatar Nov 24 '22 18:11 art049