Volbil
Anonymous
yeah but i need to , i think so.
Anonymous
i have
name = "abc"
menus = [1, 2, 3]
and this product should be linked to Menu[1], Menu[2] and Menu[3].
pls help
Anonymous
@metaprogrammer please
(sorry if this is annoying)
Rozen
poor alexander
Matthew
Good morning, is there a way to get the length of a JSONB dict in a pony query? len(r.my_dict) doesn't work
Alexander
Hi, you can write raw SQL fragments with array_length and json_object_keys like in this example:
https://stackoverflow.com/a/36173097/1706814
Anonymous
I’m still struggling with this thingy, there’s no example in the docs for how to make an instance of an entity that has many2many 😕
Alexander
Hi, let me read the previous messages
Anonymous
Thanks!
Anonymous
Anonymous
Right?
Probably try,except to catch cases that menu Nth doesn’t exist
Alexander
Yes, it looks correct
Anonymous
👌
Anonymous
Thanks a lot
Alexander
Sure
Matthew
Anonymous
pony.orm.core.OperationWithDeletedObjectError: Menu[1] was deleted
Menu[1].delete()
flush()
🤔 ☹️
Alexander
What do you mean?
Anonymous
What do you mean?
I just did
Menu[1].delete()
flush()
And i got
pony.orm.core.OperationWithDeletedObjectError: Menu[1] was deleted
Alexander
Anonymous
What is impossible?
Alexander
Anonymous
These are bots
Ah😕
By the way I thought you lost access to this account 😅
Alexander
Not yet :)
Anonymous
Oh😅💯
Maik
hey guys, i got a issue i got 2 functions they use both the decorator @db_sessions and they need to be able to run seperatly but when the first is running at the end the second must also run. now i get this error: "
pony.orm.core.TransactionError: @db_session-wrapped generator cannot be used inside another db_session"
how get i get around this?
Alexander
Currently db_session has limitations when used with async functions:
1) don't use db_session as a decorator with async functions, use with db_session inside async function instead
2) Never call async functions inside db_session, only normal function calls are possible
Maik
Alexander
async def my_function():
f1()
await f2() # you can use await here
with db_session:
f3() # only usual function calls are allowed here
f4()
await f5() # await calls a possible outside of db_session scope
with db_session:
f6() # only usual function calls are allowed here
It is not very convenient, especially considering the fact that you cannot create pony object in one db_session and use it in another db_session, but currently it works this way
Maik
ok thank you, ill try it this way
Anonymous
async def f():
with db_session:
thing = database.MyEntity()
coroutine = async_f(ABC, thing)
await coroutine
What’s wrong with that
Maik
Alexander
> What’s wrong with that
It is possible. But this is the same as
async def f():
with db_session:
thing = database.MyEntity()
await async_f(ABC, thing)
Which is also possible. But this will not work:
async def f():
with db_session:
thing = MyEntity.get(id=123)
await async_f(ABC, thing)
async async_f(something, thing):
print(thing.foo.bar.baz)
Because when you do MyEntity.get(id=123) it loads the thing itself and its direct attributes (thing.foo), but not related objects like think.foo.bar, it requires additional SQL queries. And queries are possible only inside db_session.
So, if you need access thing.foo.bar.baz later, you need to load it inside the original db_session:
async def f():
with db_session:
thing = MyEntity.get(id=123)
thing.foo.bar.baz # force loading from the database
await async_f(ABC, thing)
async async_f(something, thing):
print(thing.foo.bar.baz)
Anonymous
> What’s wrong with that
It is possible. But this is the same as
async def f():
with db_session:
thing = database.MyEntity()
await async_f(ABC, thing)
Which is also possible. But this will not work:
async def f():
with db_session:
thing = MyEntity.get(id=123)
await async_f(ABC, thing)
async async_f(something, thing):
print(thing.foo.bar.baz)
Because when you do MyEntity.get(id=123) it loads the thing itself and its direct attributes (thing.foo), but not related objects like think.foo.bar, it requires additional SQL queries. And queries are possible only inside db_session.
So, if you need access thing.foo.bar.baz later, you need to load it inside the original db_session:
async def f():
with db_session:
thing = MyEntity.get(id=123)
thing.foo.bar.baz # force loading from the database
await async_f(ABC, thing)
async async_f(something, thing):
print(thing.foo.bar.baz)
Can’t we do
def f():
ses = db_session()
with ses:
thing = MyEntity()
cor = async_f(thing, ses)
await cor
async def async_f(obj, session):
with session:
obj.foo.bar.baz
(Or with db_session() as ses if __enter__ returns self )
In order to use the original session
Alexander
No, it will not work this way. The actual session cache object is created internally and tied to the current thread. db_session was not developed with async approach in mind, and needs to be reworked to be used with async
Anonymous
Ah, ok 👌
It would be awesome, the best orm for python + async support
Maik
Alexander
and how to do this when a async is in a for loop with a db session and a await function?
Pony was not developed for async usage, and it may be tricky to use it correctly in async environment. The best way is to have dedicated thread for database access and communicate with it through queues.
> and how to do this when a async is in a for loop with a db session and a await function?
I don't know what specifically you mean, but it may be something like:
async my_function():
for item in items:
with db_session:
objects = access_database()
data = [obj.to_dict() for obj in objects]
await another_async_func(data)
Veli
does pony have GIS support? (like PointField, etc)
Maik
Alexander
Pony does not have direct support for GIS, but you can add raw SQL fragments to your queries
Veli
Veli
Alexander
Theoretically yes, but it needs to be added to declarative queries as well
Christian
On an unrelated side note, my 65-yo-mum looked at my screen earlier and remarked how "nice and orderly" the db diagram in the pony editor looked. She's anything but a tech person. I don't know where it came from, but I think this compliment belongs to you, @alexey115
Matthew
Say I have a postgres query like "SELECT * FROM mytable WHERE x = 'A' OR x = 'B' OR x = 'C' LIMIT 300;"
How can I adapt it so that the maximum number of rows where x is A is 100, and the same for B and C?
If there aren't enough rows with a certain x value, then less than 300 rows are returned overall. I don't want to do multiple queries for this.
Alexander
With Pony to do this you need to issue three independent queries and combine their result in Python.
With raw SQL queries you can do
(SELECT * FROM mytable WHERE x = 'A' LIMIT 100)
UNION
(SELECT * FROM mytable WHERE x = 'B' LIMIT 100)
UINON
(SELECT * FROM mytable WHERE x = 'C' LIMIT 100)
Matthew
good idea, thanks!
Ben
I just got a strange assertion error from within Pony
Ben
"assert t is translator"
Alexander
Can you show a traceback?
Ben
Im trying ot get one
Ben
it happens on my live server
Neal
does update table set datetime = date_add(datetime, interval) where id = 1 works like update table set count = count + 1 where id = 1 in MySQL
Anonymous
Guys, the question is this. Please tell me, is it possible to migrate data from one unique database to another (separate columns) ?
Alexander
I think what you want is called dumping?
Alexander
It is indeed an english speaking group.
You can try stackoverflow russian or smth
Ben
One question, when using optimistic=False in the db_session, does it put a lock on the database?
Alexander
No it has no relation to locks
for_update queries do
Alexander
db_session(optimistic=False) turns off adding optimistic checks for all UPDATE queries.
db_session(serializable=True) makes transactions fully isolated, and turns off optimistic checks as well. So if serializable=True then optimistic is always False
Ben
Thanks a lot! That's clear!
Ben
I keep getting AssertionError: (None, NOT_LOADED) and not sure if my Postgres server is going crazy or if something is going on with pony
Ben
for this one I have traceback
Ben
let me upload it
Ben
https://gist.github.com/benmod/f7f26a587b0ea16758b6101e228f6189
Lucky
Why would and Optional(Json) field result in JSONB NOT NULL?
Lucky
I mean if it had something like DEFAULT 'null' that would make sense...
Alexander
For JSON Optional means empty dict by default. I don't remember the reason for that, probably otherwise some queries returned strange results if in some rows the values was NULL
Lucky
So json_field is None would be translated to WHERE json_field = '{}'?
Alexander
No, it will be translated to json_field IS NULL
Alexander
Do you have some specific reason for NULL instead of empty dict for optional JSON?
Lucky
Basically I want to keep the original response around.
So I couldn't distinguish if they send me "{}" or ponyORM put that there.
Lucky
So NULL would mean they didn't send me something yet
Alexander
Maybe you can put response as a JSON item
obj.json_field['response'] = response
Lucky
Yeah. But in any way that's strange behaviour.