Object identity

10 views
Skip to first unread message

Vincent Le Goff

unread,
Mar 27, 2021, 3:54:49 PM3/27/21
to peewe...@googlegroups.com
Hi everyone,

I'm quite new to Peewee and I must admit the first tests look quite
good.  I'm looking for an ORM to support my Python-but-not-web-related
project.

So my first question is regarding object identity and is quite simple:
when you get/select from the same database, do you get the same objects
(in terms of identity) or new ones?  And can it be changed?  My current
project is quite flexible, but it's a bit restrictive in that area and I
would prefer to have the same objects to deal with.  In other words,
Person.get(id=1) is Person.get(id=1) should always return True.  This is
not as straightforward and a lot of ORMs I've seen just create new objects.

One of the reasons why I'd like to keep the same objects during the
whole connection lifetime is that I usually store things (like handlers)
on instances.  I'd a great fan of handlers and I would really feel sad
to let them go.  So for instance I usually have a TagHandler defined as
a property (or a lazy property, to be exact) on the model class.  So I
can do person = Person.get(id=1); person.tags.add(...).  The mechanism
behind the tag handler is a bit complex (because tags can be applied to
several classes), but the important point here is that, if I get a
different object each time, the tag handler will need to be recreated
each time too. And I do have lots of handlers.  I'm not psyched about
performance.  Caching has its cost, to be sure, it has to be a smart
balance, but I'd rather deal with the same objects.  If object identity
is not guaranteed, I'll have to find a way to cache my handlers
somewhere else (maybe in the class itself).

I have other questions before deciding if I should switch to Peewee for
my current project, but it's not exactly critical right now.  The
question over object identity may be the most important one.

Thanks for your answers!

Happy hacking,

Vincent

Charles Leifer

unread,
Mar 27, 2021, 5:26:09 PM3/27/21
to peewe...@googlegroups.com
Peewee is active record so they are different objects even if they correspond to the same logical row. In practice this works well for many usages and usage patterns, but as you pointed out, not all.

If you want id mapper then I'd recommend sqlalchemy instead.

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/dfa8b35e-f3c8-dc20-0f45-17d6ab591915%40gmail.com.

Vincent Le Goff

unread,
Mar 28, 2021, 7:04:52 AM3/28/21
to peewe...@googlegroups.com

Hi Charles,

Thank you for your prompt answer.  To tell you the truth, I'm not a huge fan of SQLAlchemy's syntax, in particular regarding queries.  What Peewee offers seems much more intuitive to use, at least in my opinion.

Would it be any possible to play with the Model class, say, if I inherit from it while replacing some behavior?  The call to some special methods is involved after all, and I'm not against creating a basic ID mapper if that comes to that.

Also I've realized part of the problem for me is that I store objects beyond a transaction.  For instance I tend to cache the location of characters (characters or locations are defined in the database, somewhat loosely because, again, either can be a different class).  So that if char1.location calls the database each time, it might prove a bit problematic.  Perhaps, however, I can manage to cache first call to char1.location in the transaction, and end the cache when the transaction ends, to avoid problems.  Some engines do not mind if we store objects beyond a transaction, as long as we "don't modify them", but it might create synchronization issues, where an old "cached" object has some fields set but the actual store object in database has others.  Again, an ID mapper here would solve this, as there is only one object to represent a single row, and modifying one calls an update on the database.  Therefore, if it's not too much trouble creating an ID mapper (if, in fact, this is possible with Peewee) I wouldn't mind trying.  If not possible, I'll have to weight whether the absence of an ID mapper would make things too difficult and changing ORM would be necessary, or whether I can work without ID mappers.

Thanks again for your answer,

Vincent

Charles Leifer

unread,
Mar 28, 2021, 11:46:07 AM3/28/21
to peewe...@googlegroups.com
I think it's going to be harder than you think and there will be lots of sharp edges if you try that. Session lifetimes are really important for id-mapper, as well as ensuring that any objects returned by queries correspond to the same logical row you may have kept in-memory. Peewee does not have any facilities for tracking models in this way. It's very much built around the assumption that a model instance is just a container for the data.

Reply all
Reply to author
Forward
0 new messages