Permalink Bot
Anonymous
Thanks, @metaprogrammer, that was quick! :-)
Vitaliy
Great! I also waited for this update. 🙂 When should the next release be available?
Anonymous
@metaprogrammer, I'm away from the computer right now, but it seems like your assert in line 2497 will fail if the class has no derived objects. I don't feel this is right. I think that the following should pass in your test file:
select(g.number for g in Group if isinstance(g, Group)).
It would just feel more pythonic this way, I think.
Anonymous
If it already works and I don't see it, you should probably just add such test, just to be safe.
Anonymous
I'm sorry for complaining, don't get me wrong, you're doing a great job. :-)
Alexander
Krzysztof, no need to apologize, I appreciate you feedback, it is on-point and constructive. Sometimes I make stupid errors, and It is great when someone reviews the code for errors
I just added test you suggested.
assert in 2497 will not fail, because in that case Pony recognizes that isinstance check makes no sense and replace discriminator check with dummy WHERE 1 = 1 expression (or with WHERE 0 = 1 for queries like select(p for p in Person if isinstance(s, Group)))
Anonymous
OK, that is great. Thanks once again! :-)
Alexander
@vitalium I don't know yet when will be the next release date, I think around October 20. I want to add a bit more features in it.
Anonymous
@metaprogrammer Just one more thing you need to change in test_isinstance.py: rename one of the test_8 functions to test_9 (there is two of them). Except that, it's all great as always. :-)
Jorge
Hey, i have a question
Jorge
Is there some way to get the information of an object attrs that is a Set or a referencr of another object outside a db_session? Currently i extract hand made all the info i need of an object and return a dict
Anonymous
It seems like your issue has one answer on StackOverflow (see bottom part, about “touching” the attributes):
https://stackoverflow.com/questions/25719869/databasesessionisover-with-pony-orm-due-to-lazy-loading
Anonymous
Have you checked such solution? If you have, why wasn't it enough?
Anonymous
Oh, and there is the second answer that I missed, my bad. See second top answer, from @metaprogrammer, about the prefetch function.
Anonymous
In fact, you could consider both approaches and see which is a better fit.
Jorge
Jorge
I thought that put the dbsession in top level was bad habit
Jorge
but, if is the correct way, i tried it
Alexander
Hi Wasdf! Yes, it is the correct way to wrap db_session around the entire processing, including template generation, etc.
Krzysztof, thanks, I've fxed the typo in the test name
Jorge
It works well
Alexander
👍
Jorge
with prefetch, is there any way to get a Set of entities?
Jorge
btw i have no problem with de db_session top level option xD
Alexander
Jorge
Alexander
No problem
Jorge
```
class A(db.Entity):
id = orm.PrimaryKey(int, auto=True)
b = orm.Set(B)
class B(db.Entity):
id=orm.PrimaryKey(int, auto=True)
a = orm.Set(A)
Alexander
It should work:
from pony.orm.examples.university1 import *
populate_database()
with db_session(sql_debug=True):
students = select(s for s in Student if s.gpa > 3).prefetch(Student.courses)[:]
for s in students:
print(s.name)
for c in s.courses:
print(c.name)
print()
Note [:] after the query, it forces the query execution inside db_session. Without [:] of .fetch the query will be lazy and don't prefetch anything
Jorge
Jorge
Jorge
Thank you very much! i recommend everybody to take a look pony orm, awesome
Alexander
👍
Jorge
sorry, it is possible to make 3 level join in a select? I want to get Diary object and name attr of Person. Diary points Contact and Contact to Person
Person - Contact - Diary
select((d, p.name) for d in Diary for p in c.contact.person if blabla
)
But pony says
NotImplementedError: for p in c.contact.person
Jorge
currently im iterating to get the value
Jorge
just asking if its possible
Alexander
This is because d.contact.person is not a collection, but a single object, so you cannot iterate over it
You can just write:
select((d, d.contact.person.name) for d in Diary if something)
Alternatively, you can write it in reverse direction:
select((d, p.name) for p in Person for d in p.contacts.diary if something)
Alexander
Or you can write
select((d, p.name) for d in Diary for p in Person if p == d.contact.person and something)
Jorge
Jorge
Jorge
thanks again XD
Alexander
Sure
Jorge
is more simple than i thought
Jorge
Good morning! There is a good pattern design to apply and work with ponyorm (or an another python orm)? Im relatively new on python, im searching on internet but just in case..
Alexander
Hi! I don't know any specific design patterns for use with orm. PonyORM itself uses multiple design patterns, such as
IndentityMap https://martinfowler.com/eaaCatalog/identityMap.html
UnitOfWork https://martinfowler.com/eaaCatalog/unitOfWork.html
LazyLoad https://martinfowler.com/eaaCatalog/lazyLoad.html
QueryObject https://martinfowler.com/eaaCatalog/queryObject.html
etc
Jorge
Just asking to get a good logic design layers, i have a look to these links
Carel
When using Pony, with say Tornado, i instantiate the database in one file, declare the tables in another and Do all he manipulations in my other files. This allows one to pass the database object around as necessary and lets you selectively use particular models. Also it’s as close as I could get to using pony/tornado as one might use models in Django.
Jorge
Jorge
i mean
Jorge
what i understand to good design is that is necessary to make every layer do one thing and relegate to another layers other things, to work with db i thought that is a good idea have an access db layer with all methods wich access to the entities and then all the files or ocmpoents access to that access layer
Jorge
im a newbie, im just asking
Jorge
xD
Alexander
I think PonyORM is *already* that separate DB layer that abstracts database usage from application code.
In my practice, any separation of additional db layers leads to serious code complications, and looks like an anti-pattern
Alexander
Additional separation make sense when you need to pass information to another process, for example, to JavaSctipt front-end layer. Then you need to convert retrieved entities to JSON structures
Carel
Not sure what that is. But it, in combination with relative imports (Python > 3) allows you to pull in the models wherever it is necessary. So you can use “from . import models/database” everywhere within your package. It is a bit tricky to setup. I don’t have a public repo of this at the moment I’m afraid.
Jorge
Jorge
thanks a lot guys
Alexander
Carel, regarding import of db/models, it is also possible to import just db object and then access entities as an attributes of db object. Sometimes it may simplify imports
from pony import orm
from models import db
with orm.db_session:
result = orm.select(p for p in db.Person)
Jorge
Alexander
The drawback is IDEs (like PyCharm) don't understand what db.Person is and auto-complete Person attributes may not work
Jorge
Jorge
i like the concept of static tiped in other languages, is a pain in the ass the object inspection in python imho
Jorge
and, i have a last question, wich is better, pass the db object to threads class i launch or import the db on all the threads?
Jorge
i see on the docs ponyorm is thread safe
Alexander
I think there is no difference between importing and passing of db object, use what is more convenient
Alexander
Also, do you write web application or desktop application?
Jorge
mm, service-backend application?
Jorge
in the next weeks i will integrate with flask
Jorge
but now only is a service wich read/writes the db and make thinks with the data
Carel
@akozlovsky thanks. I’ll double check what I did in my code. I recall I needed to throw the dB instance about somewhat, hence the separate package structure.
Anonymous
@ask does pony have a tool to generate from raw sql to pony object?
Alexander
To generate what?
Or you mean to generate raw sql?
Anonymous
From raw sql into pony object
Alexander
You mean generate entities from tables declaration?
Anonymous
Yes
Anonymous
I do not speak english well
Alexander
https://github.com/abetkin/pony-inspect
Anonymous
The case if I have already make tables in sql languages and I want to try Pony and use pony query.Is there any tools to convert tables into pony class?
Alexander
If I remeber correctly you can use this.