Roman
No. We'll fix it tomorrow
Joery
Another thing I noticed is that the 'Max length' property does not work
Alexander
Doesn't work in Pony or designer?
Joery
Henri
/help@bonbot
Matthew
Hi, any thoughts on Pony implementing BETWEEN for postgres?
Matthew
I realise that it is the save as a >= x AND a <= y, that is not in pony either though
Alexander
It actually looks easy to implement, I mean between(a, x, y). I agree it may be convenient, I'll try to add it the other day
Matthew
it looks like it exists in sqlite and mysql as well
Matthew
there is also NOT BETWEEN
Lucky
Matthew
wouldn't that turn into an IN (13, 14) SQL currently?
Matthew
btw this is also useful for dates / datetimes as well as numbers
Lucky
Matthew
select(u for u in User if u.id in range(1, 10))[ : ]
Matthew
turns into
Matthew
...
FROM "user" "u"
WHERE "u"."id" IN (%(p1)s, %(p2)s, %(p3)s, %(p4)s, %(p5)s, %(p6)s, %(p7)s, %(p8)s, %(p9)s)
{'p1':1, 'p2':2, 'p3':3, 'p4':4, 'p5':5, 'p6':6, 'p7':7, 'p8':8, 'p9':9}
Alexander
Theoretically it is possible to translate range(i, j) to BETWEEN too. But I'm not sure we need, because range is limited - its arguments should be integers. I think it is better to add between function to pony.utils and learn pony to translate it to SQL.
Alexander
I added between and pushed to GitHub:
from pony.orm.examples.university1 import *
select(s for s in Student if between(s.gpa, 3.5, 4.5))[:]
select(s for s in Student if not between(s.gpa, 3.5, 4.5))[:]
Matthew
Cool! Will it work with dates / datetimes?
Alexander
I think yes. It should work with any types for which you can use SQL BETWEEN expression
Matthew
and is "not between" implicitly supported?
Alexander
yes
Matthew
+def between(a, x, y):
+ return a <= x <= y
+
Matthew
Does that mean that select(z for z in Z in z.a <= x <= z.y) will work with Pony?
Alexander
At this moment - no, we need to add support to decompiler, it is a bit untrivial. The body of between function is not used during translation, it just to be able to use it in Python
Matthew
ah ok
Alexander
SQLite:
>>> select(s for s in Student if not between(s.dob, date(1995, 1, 1), date(1998, 1, 1)))[:]
SELECT "s"."id", "s"."name", "s"."dob", "s"."tel", "s"."gpa", "s"."group"
FROM "Student" "s"
WHERE "s"."dob" NOT BETWEEN '1995-01-01' AND '1998-01-01'
[Student[1], Student[2], Student[3], Student[4], Student[5], Student[6], Student[7]]
>>>
Matthew
Cool :)
Matthew
Pony is becoming very solid now
Matthew
in terms of features and reliability
Alexander
I plan to work on Pony until New year and add many missing features - migrations, upserts, etc.
Matthew
How could an upsert look in Pony?
Alexander
obj = MyObject.create_or_update(foo=1, bar=2)
and
parent_object.some_collection.create_or_update(foo=1, bar=2)
Matthew
Cool, I often do x = X.get(foo=1) or X(foo=1) (or kwargs passed to both), but I imagine upsert would be better
Alexander
The main benefit of upsert is atomicity. X.get(foo=1) or X(foo=1) sometimes can raise an exception, if another transaction was able to insert object in between
F
Hello guys, I wonder if the source code for the online editor Is available , or if you could tell me which js libraries is using to render the diagrams and svg connections. I'm trying to start an open source project to do a similar think for Django models and it would be very instructive. Thanks
Alexey
Hi Federico,
we didn't open source the editor code.
We use reactjs, mobx and svg.
The next week we are releasing the editor for Django - want to see if it will be useful for Django community
F
Good to hear that, I'll stay tuned for that. I also wanted to generate the code to wire up those models to api views with django rest or graphene, so perhaps I'll also try to build the diagram myself. Thanks!
Alexey
Great! Will let you know once we deploy it.
The only thing that I'm afraid will not be very straitforward is that we have to split the Django models declarations and the behavior the way it is described here https://bradmontgomery.net/blog/django-models-mixins-for-cleaner-code/
Alexey
because each models update will rewrite the file, and having the model methods incorporated into the models is not convenient
Anonymous
is there a way in pony to detect duplicate records on insert? such as
INSERT INTO the_table (id, column_1, column_2)
VALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')
ON CONFLICT (id) DO UPDATE
SET column_1 = excluded.column_1,
column_2 = excluded.column_2;
Anonymous
using postgres
Micaiah
Does Pony have something like table_entry.update(**newdata)
Alexander
is there a way in pony to detect duplicate records on insert? such as
INSERT INTO the_table (id, column_1, column_2)
VALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')
ON CONFLICT (id) DO UPDATE
SET column_1 = excluded.column_1,
column_2 = excluded.column_2;
At this monent you need to write raw sql query using db.execute(sql):
https://docs.ponyorm.com/api_reference.html?highlight=execute#Database.execute
Micaiah
Anonymous
По русски здесь говорят?
Alexander
No, please speak in English
Anonymous
Ok
Lucky
Matthew
Is there any reason why an after_insert method on a pony entity would not fire in a flask application?
Matthew
the objects are successfully created in the database, but the after_insert isn't called (verified with a print statement on first line of after_insert method)
Alexander
Can you define after_update method too and check if it is called instead?
Matthew
Should it be called on the creation of a new object?
Matthew
after_insert seems to work in other parts of the same project
Matthew
i think this is not pony's fault :)
Alexander
It should work. The behavior that you described looks like a bug. It was my guess that maybe another object X was able to update the object Y you speak about in its X.after_insert hook, and than Y.after_update hook was invoked instead of Y.after_insert. It just a wild guess. Maybe the problem lies somewhere else
Matthew
after mentioning it, i noticed that the method was being called
Alexander
Ah, ok. Good
Matthew
is after_insert always called after commit (either explicit or implicit) ? I am getting background jobs created in an after_insert unable to find newly created objects (which had after_insert fired) running in another process
Matthew
The job does get a numeric ID though
Matthew
which implies that the commit has happened
Matthew
The workers are python-rq
Lucky
Can I do mass-update statements in pony?
I've got Stickers in a Pack, having tl_ids.
I have a list of stickers which should not be set to deleted, namely still_existing_sticker_tl_ids=set(123,456,789)
SQL should be something like this:
"""
UPDATE sticker SET deleted=true WHERE
sticker.pack == %db_pack% AND sticker.tl_id NOT IN (%still_existing_sticker_tl_ids%)
"""
I think the Pony orm select is roughly
query = orm.select(db_sticker for db_sticker in db_pack.stickers if db_sticker.tl_id not in still_existing_sticker_tl_ids)
Matthew
you can do query.delete(bulk=True)
Lucky
I set a delete flag, not actually remove them
Matthew
oh sorry
Lucky
No problem :D
Matthew
afaik no that's not currently possible in pony
Lucky
So there is no select(...).update(field=lel)
Matthew
not that I know of
Matthew
When pony does a query (using postgres), using a long running session, is it guaranteed to pick up objects which have just been committed in another session?
Lucky
Maybe using orm.commit?
Matthew
I do this in one session:
X(a=1); commit()
Then in another existing session, I do X.get(id=1)
Matthew
where 1 is the ID of the object just created
Matthew
and then it doesn't find it
Matthew
I have recently discovered this as an issue because the work queue is more likely to be empty, due to efficiency improvements
Matthew
could serializable=True be the answer?
Matthew
alternative could be scheduling work in say 10 seconds but that feels ugly