Kyle
During the session user can make multiple actions with db access
Kyle
Instead of storing all values I would prefer to keep a instance of the class
Kyle
Can this be done? or I'd need to get the object to update ?
Kyle
I'm currently migrating from django orm, in django I can store a orm object and call save() whenever I want to commit changes I made
Alexander
In Pony all modified objects are saved automatically at the end of db_session. The benefit of this is that it is not necessary to manually track which objects should be saved The drawback is that you don't have manual control of which updated objects to save (usually it is not necessary, and all modified objects should be saved) Currently Pony doesn't allow to "disconnect" object from db_session. So you probably need to store modified data inside a dict, and at the end of user session find user object by id and update its fields
Kyle
I see
Kyle
Is there a function to get all data from an object? and also one for setting it?
Alexander
for getting data you can use obj.to_dict() https://docs.ponyorm.org/api_reference.html#Entity.to_dict for setting you can do: for key, value in d.items(): setattr(obj, key, value)
Kyle
Great, thanks!
Kyle
class ORMWrapper: def __init__(self, obj): self.__dict__ = obj.to_dict() self.ORMClass = type(obj) def save(self): with db_session: obj = self.ORMClass.get(id=self.id) for key, value in self.__dict__.items(): if key == 'ORMClass': continue setattr(obj, key, value) def delete(self): with db_session: obj = self.ORMClass.get(id=self.id) obj.delete()
Kyle
Just made that to make my life easier ^^
Kyle
Is select(p for p in Product if p.price > 100) faster than Product.select(lambda p: p.price>100) ?
֎𝚈𝚎𝚑𝚞𝚍𝚊֍
hi, i'm trying to do something like: select((p.name, p.price) for p in Product).sort_by(lambda p: p.factory) but i get: TypeError: Incorrect number of lambda arguments. Expected: 2, got: 1
֎𝚈𝚎𝚑𝚞𝚍𝚊֍
why? 🤔
Alexander
You select (p.name, p.price) this is a tuple of two elements. They will be used in lambda function
֎𝚈𝚎𝚑𝚞𝚍𝚊֍
so how can i get 2 elemets from class Product ?
֎𝚈𝚎𝚑𝚞𝚍𝚊֍
select((cc.content, cc.content_type) for cc in CommandContent if cc.command.name == name).sort_by(lambda cc: cc.order)
Matthew
so how can i get 2 elemets from class Product ?
select the whole “p” row and then use “p.name” etc in your code after the query
v4ldevrr4m4
hello people Let me ask for some references ----> I was searching conparatives until found this presentation in 2015 https://es.slideshare.net/jmoc25/python-comparing-orm-55394354 It is in spanish that is my native language. Ok in this presentation PONY got best good comments incluiding performance. This is the question: Some of you do have a more recently an interesting comparative
v4ldevrr4m4
All people here agree than PONY is better to SQLALchemy ? :D :D
Alexander
Hi! I think both PonyORM and SQLAlchemy have strong points :) Pony API is easier to use, and Pony is usually faster. SQLAlchemy has bigger community, working migrations and more flexibility. Regarding performance comparison you can look here:
Alexander
Hey guys! I recently decided to compare the performance of pony ORM with SQLAlchemy. I posted results on Hackernoon: https://hackernoon.com/performance-testing-of-python-orms-based-on-the-tpc-c-benchmark-ae3g3yis Your feedback is very appreciated
v4ldevrr4m4
☝️👌 thanks Alexander
André
I think adding .pyi files is possible
Doing that right now in a PR. I based off of orm branch.
André
https://mypy.readthedocs.io/en/stable/python2.html would be an alternative, but I go with .pyi files
Edis
hey there, I'm using pony orm now for the first time. As a far as I understand I just need to create my entity object in a db_session and it will trigger an insert when the session is getting closed, right?
Edis
Idk why but when I create the object nothing happens and the things that should happen after that aren't executed, the function just stops
Edis
and I don't get any message
Edis
I even have debug messages activated and tried to execute it with a debugger
Edis
but i can't see why it just stops at that point
Edis
does anyone know why that might happen?
Edis
i figured it out but i got another issue now
Edis
i created a many to one relation and it says that the Entity definition doesn't exist
Edis
For the first entities I created a many-to-one relation I didn't have this error
Edis
But for this I do, and I don't know how to fix it
Edis
class Goal(db.Entity): match = Required("Match") scorer = Required("Player") minute_scored = Required(int) composite_key(match, scorer, minute_scored) class Match(db.Entity): id = PrimaryKey(int) opponent = Required(Team) venue = Required(Venue) our_score = Optional(int) opponent_score = Optional(int) starting_at = Optional(datetime) is_full_time = Required(bool, default=False, sql_default=False) season_start = Required(int) competition = Required(str) matchday = Required(str) goals = Set("Goal")
Edis
pony.orm.core.ERDiagramError: Entity definition Goal was not found
Alexander
Hi! Does Goal and Match entities belong to the same db object?
Edis
yes
Edis
but player belongs to another
Edis
could that be the problem?
Alexander
All entities which have relationships should belong to the same db object
Edis
alright, i'll try to use only one db object and see if it works
Edis
i wanna define the entities in different files
Edis
is that possible?
Alexander
Yes
Alexander
https://stackoverflow.com/questions/51601838/ponyorm-multiple-model-files
Edis
thank you
Edis
it worked
Edis
i guess though i'm having another issue
Edis
I'm using an Enum
Edis
I created a database converter now
Edis
But I can't add it
Edis
db.provider.converter_classes.append((Enum, EnumConverter))
Edis
AttributeError: 'NoneType' object has no attribute 'converter_classes'
Alexander
Converter is only a small part of what is necessary to add a type Probably you can use a simpler alternative, like class Colors: RED = 1 BLUE = 2 GREEN = 3 class Person(db.Entity): eye_color = Required(int, default=Colors.GREEN)
Edis
👍
Volbil
Hello guys, I just found out about Pony and decided give it a try
Volbil
And here is what I got ImportError: cannot import name 'orm' from partially initialized module 'pony' (most likely due to a circular import)
Volbil
Does anybody have an idea what may cause this issue?
Alexander
What Python and Pony version do you use?
Volbil
Python 3.8.5 and pony 0.7.13
Bloo
Yay Python3.8! Loving it! :)
Volbil
Oh, I know why it's happening
Volbil
I named my file for testing pony.py lol
Volbil
My bad
Alexander
Ah, ok
Edis
What would be an equivalent to this sql query in pony orm? I'm still having problems getting used to the syntax
Edis
sql SELECT * FROM Match WHERE season_start = (SELECT MAX(season_start) FROM Match);
Edis
Match.select(lambda m: m.season_start == max(m.season_start)).order_by(Match.starting_at)
Edis
currently i have this, but it doesn't work
Edis
I mean I could write it in sql too but I want to stick to use python for everything
Matthew
… == max(m2.season_start for m2 in Match)
Matthew
that makes it a subquery
Matthew
that should work
Edis
thank you
Edis
In my entity I set a datetime attribute, when I get it from the db it is a str though