Comments (5)
Thanks, this is bug, will be fixed today.
from pony.
Here is why that happened:
Each Pony ORM session has a cache. This cache contains:
- objects loaded from the database;
- query results.
The query results cache was not cleared after commit()
, that was a bug. Since you call the same query twice, the second time query result was taken from a cache.
Your last query looks the same, but its generator expression has separate Python code object, so from Pony point of view it is a different query. Because of this, the query result was taken not from the cache, but from database.
Now, after the bug is fixed, query results cache is cleared correctly after commit()
, rollback()
or flush()
.
Note, however, that objects cache are still not cleared after commit()
, only query results are cleared:
u1 = User[123] # The object is cached
...
commit() # Query results cache is cleared, but u1 are still cached
print u1.email # You can still access cached user object
rollback() # The cache is invalidated now
print u1.email # TransactionRolledBack: Object belongs to obsolete cache
To clear object cache you should wrap the function which works with the database with @with_transaction
decorator:
@with_transaction
def create_users():
for i in range(2):
User(email="email%d" % i, password="password%d" % i)
# commit() is unnecessary here
create_users()
# commit() and cache clearing will be done automatically
from pony.
so quick, cool, thx
sth about the cache, i got it, but there is another question
why the parent iterator have effect on the select(generator)
for i in range(2):
select()
will select form db once;
select()
select()
will select form db twice;
from pony.
When Pony caches query result it uses id of generator's codeobject as part of the cache key.
In the first case, you call select()
with the same generator twice, and on the second iteration Pony uses query result from the cache.
In the second case, you call two different generators, and Python creates two different codeobjects with different ids. Currently Pony doesn't recognizes such generators represent the same query and send two separate queries to the database. In future we may change the way query key is generated and after that Pony will take result of the second select()
from the cache.
The next example demonstrates that different generator codeobjects have different ids:
>>> names = [ 'John', 'Smith', 'Bob' ]
>>> def f1():
... gen1 = (name for name in names)
... print id(gen1.gi_frame.f_code)
...
>>> def f2():
... gen2 = (name for name in names)
... print id(gen2.gi_frame.f_code)
...
>>> f1()
54873504
>>> f1()
54873504
>>> f2()
54873144
>>> f2()
54873144
>>>
from pony.
ok, thanks for you answer
from pony.
Related Issues (20)
- SQLite: JSON entity doesn't meet Python Standard HOT 1
- How to combine two `query.filter()` calls using `OR` operator? HOT 3
- pony.orm.decompiling.DecompileError: Unsupported operation: POP_JUMP_IF_NONE
- Python 3.12 support HOT 1
- test_json fails with sqlite 3.45 HOT 1
- can l use pony orm with flask admin library?
- Using sqlite extensions
- error constructing query from stored subqueries HOT 1
- Disabling automatic persistence
- Django Integration
- select - decompiling - errors HOT 1
- Column aliasing in PonyOrm's global select() function
- How to integrate with Dependency Injector lib
- Auto-complete on entity construction when using intellisense (VSCode) HOT 1
- Database property to check if mapping has been generated
- how to use pony orm in fastapi app&sync router,any demo code ?
- Better support for default schemas
- Support Views and Materialized Views
- date-type field not automatically converted from text to date when using SQLite (?)
- Python 3.12 not supported HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pony.