Alexander
https://del.dog/ looks nice
Anonymous
https://del.dog/ looks nice
Yes you can also add .py/.any-other-suffix to the url to get it colored :D
Anonymous
By the way, there are many(?) asserts without message, it would be nice if you could add a message to explain what’s wrong please
Alexander
An assert in Pony means that the error should not happen, it's like a comment "I'm sure that variable should have this state, or else it is definitely a bug". They are not errors that should be presented to a user
Anonymous
ok
Karsten
Hi, I have the following problem. I am filtering for an empty cell. Here is the code: class BlzEvent(db.Entity): _table_ = 'blz_events' id = PrimaryKey(int, auto=True) # 0 - hidden ... event_time = Required(datetime) # 4 ... action = Optional(str) # 21 - visible for administrators t_from: datetime = t_from t_to: datetime = t_to select_string: str = '(r.event_time >= $t_from) and (r.event_time <= $t_to) and r.action = '')' with db_session: result = select(r for r in BlzEvent if raw_sql(select_string)) result.fetch() message : wrap_dbapi_exceptions raise OperationalError(e) pony.orm.dbapiprovider.OperationalError: near ")": syntax error r.action is empty (not zero !! but '') this query works without an error message : '(r.event_time >= $t_from) and (r.event_time <= $t_to)' what am I doing wrong ? greeting with db_session: result = select(r for r in BlzEvent if raw_sql(select_string)) result.fetch()
Alexander
and r.action = '')
Alexander
this closing parenthesis does not have corresponding opening parenthesis
Karsten
Alexander
If you use single quotes inside your string, you need to use double quotes for quoting the string itself, or use backquotes: "(r.event_time >= $t_from) and (r.event_time <= $t_to) and (r.action = '')" '(r.event_time >= $t_from) and (r.event_time <= $t_to) and (r.action = \'\')'
Karsten
Oh thank you ! I should have known that myself. I come from C #, a lot is new here! thanks
Zek
There is an issue in pony editor. Some attributes are disappearing from the view however they are present in the code.
Alexander
In what diagram? Is it private?
Zek
no
Zek
zekiblue/ih
Zek
table: user, attribute: id
Alexander
Each entity should have a primary key. If it is not specified explicitly, then an implicit id = PrimaryKey(int, auto=True) attribute used instead.
Alexander
You can specify another primary key
Zek
Ah okey, now its fixed. Thanks Alexander
Anonymous
date = Required(datetime, nullable=True) Seems like Required and nullable don't love each other
Anonymous
ValueError: Attribute Ad.date is required [ad.date = None]
Volbil
You should use Optional
Anonymous
oh, ok. so it will be better to raise an error by the time i do Required(... nullable=True) instead of the see the errors only when i add value to DB XD
Lucky
Yeah, that is really confusing, especially with the dear friend of Strings behaving differntly.
Adam
im working with flask and im getting "pony.orm.dbapiprovider.OperationalError: no such table: "
Matthew
double check that you are connected to the right database and the tables exist within that database
Adam
only one database defined and i did call create_tables
Adam
if i do show() right before it, it shows that its there and defined correctly
Alexander
What table name is in error? Is it a table for some entity?
Anonymous
let's say i have Car.a_property can i do an hook / cause a_property to act like a descriptor? so i can define __get__ and every time i will access Car.a_property so __get__ will be called and will return a value?
Adam
What table name is in error? Is it a table for some entity?
figured it out, was trying to use :memory: and i guess it didnt like that, as when i changed to using a file it worked.
Anonymous
how can i get all fields of an instance of entity? i've tried with vars() but i get {}
Matthew
dict(x)
Alexander
I think you mean obj.to_dict()
Matthew
ah yes 🙂 long day
Alexander
Internally Pony object stores attribute values in obj._vals_, but you should not access it directly
Anonymous
👌
Anonymous
weird behavior, my after_update and before_update get called many times, without any change. [a stands for AFTER and b for BEFORE]
El Mojo
Guys, sorry for insisting, are you planning a new release for the async part soon? I need to know if I can start a new project on fastapi / pony and wait for a release the next month or just go with sql alchemy. Thank you very much for your assistance
Alexander
Alexander
let's say i have Car.a_property can i do an hook / cause a_property to act like a descriptor? so i can define __get__ and every time i will access Car.a_property so __get__ will be called and will return a value?
Pony attributes used in Python as well as in queries, and so Pony should be able to translate them to SQL. If you want to add some property with complex logic, it cannot be used in SQL. So you can add additional properties alongside with normal Pony attributes, but you should not completely replace attribute with property. You can do something like: class Person(db.Entity): first_name = Required(str) last_name = Required(str) @property def full_name(self): return self.first_name + ' ' + self.last_name @full_name.setter def full_name(self, value): self.first_name, self.last_name = value.split()
Anonymous
Maybe you are assigning attribute values in after_update (which you should not do)
no, i don't. maybe apscheduler does something weird with the obj.
Anonymous
i was updating values in another function, not in after_update. sorry for bothering
Anonymous
hmm, so my problem was that i had to do ad.already_posted = True and my scheduler get all ads which already_post == False. what can i do to mark my ad as posted and avoid after_update calling?
Anonymous
seems like i can do an hack like def after_update(self): if self.already_posted: return but i prefer a better way like already_posted = Optional(bool, do_not_call_after_update=True) [do_not_call_after_update is a random name xd]
Alexander
Probably it should work
Anonymous
yes, but it's a bit "hacky"
Anonymous
in my Entity, i have the following column where_to_post = Required(IntArray, default=?) by default i want it to contain a list of few ID's which will be taken from another Entity, i.e i have this entity as well: class Channel(database.Entity): id = PrimaryKey(int, min=-10000000000000, size=64) and i want where_to_post to contain a list of Channel.id, like default=list(Channel.select(lambda c: c.id)) is it even possible?
Anonymous
pony.orm.core.ERDiagramError: Mapping is not generated for entity this is the error i get when i do that
Alexander
You need to do db.generate_mapping() before performing selects
Anonymous
You need to do db.generate_mapping() before performing selects
so i will call it twice, once before the entity which contains where_to_post and once after, right?
Alexander
No, just a single time after all entities are defined
Alexander
> is it even possible? No, you can't put such things in default, but you can explicitly assign this items on object creation You can define a factory function for object creation. You can even do it a classmethod: class Channel(db.Entity): where_to_post = Required(IntArray) @classmethod new_channel(cls, foo, bar): return cls(foo, bar, where_to_post=<some expression>)
Anonymous
ok, cool thanks
Anonymous
seems like def __init__(self, *args, **kwargs): with db_session: super().__init__(*args, **kwargs, where_to_post=list(Channel.select(lambda c: c.id))) should work
Alexander
db_session definitely should not be placed here
Anonymous
oh ok, so ill remove it
Alexander
But yes, you can override __init__
Alexander
db_session should wrap an entire lifecycle of in-memory Pony object
Anonymous
Is there anything like graphene-sqlalchemy for pony? Could not really find anything.
꧁🦔
Hi! I need to make many similar queries like
꧁🦔
SELECT count(city) as cnt, city FROM store.users GROUP BY city ORDER BY cnt DESC
꧁🦔
moscow, 123 new-york, 99 ......etc... how to do it with PonyORM?
Alexander
select((u.city, count()) for u in User).order_by(-2)
꧁🦔
а что значит -2?
꧁🦔
-1 последний аргумент, а -2 - предпоследний?
Alexander
order by second column of result in descending order