Alexander
yes. You can use product.results.count() instead, in that case Pony will issue select count(*) ... query without loading all items into memory
Matthew
thanks, I think this is probably the issue then
Matthew
So with count(), the query that is failing would now be the first time .results is requested
Alexander
Yes, the query that is failing will load just added SpyResult object but as the collection is not fully loaded it will not raise any exception
Matthew
Great, thanks for your help
Alexander
Sure
Matthew
Would you say it's a good pattern to always do a count() query rather than len() of a normal query, to avoid this issue?
Alexander
I think it depends. Sometimes you want to iterate over all items anyway, and in that case count() query will be unnecessary, and count() result may differ from the actual number of collection items you get during iteration
Alexander
If you don't plan to iterate over items, i think count() is better
Matthew
Right, I'm just checking if count() == 0
Alexander
If you want to just know is some collection empty or not, the more efficient way is just check if product.results.is_empty()
Matthew
But if it's a filtered query, not just the whole results set, is_empty() seems not to be possible? So count() would be best?
Alexander
For query it should be select(...).exists() or product.results.select(lambda: ...).exists()
Alexander
Maybe we need to make API a bit more uniform, I don't know
Matthew
My knowledge of the API hasn't kept up to date :)
Matthew
I do things "the old way" a lot of the time i think
Max
Hi, guys... Who can provide simple example of group and join multiple tables with model... Like this Model.Select().join(Model)...How i can get only group without using aggregation function? Why Model.delete() is not work... Model.select().delete() work...
Max
PS... I know what Alexsander and Alexsey are core developers of pony) They only answers on the questions...
Alexey
Hi, guys... Who can provide simple example of group and join multiple tables with model... Like this Model.Select().join(Model)...How i can get only group without using aggregation function? Why Model.delete() is not work... Model.select().delete() work...
Here is example of left_join https://docs.ponyorm.com/api_reference.html#left_join Model.delete() wouldn't know what object to delete - you need to call delete() on entity instance (an object), not an entity (class)
Max
Happy holiday)
Max
Alexsey, do you can update pony docs with more powerfull example https://docs.ponyorm.com/working_with_entity_instances.html
Max
Yes, it does not know.. But if is not logically.. if coder dont select objects thеn tool remove all data from table
Alexey
Here you can find more examples: https://github.com/ponyorm/pony/blob/orm/pony/orm/examples/estore.py https://github.com/ponyorm/pony/blob/orm/pony/orm/examples/university1.py
Max
Aleksey , thanks..
Max
example only for select... where is working with model entity and lambdas?
Alexey
most queries that return an entity instance could be written using lambda - just put the condition part from the generator query to lambda here are more details on using generator experssions and lambdas https://docs.ponyorm.com/queries.html
Max
Ok, examples were in other category...
Max
Values_list? Model.select().values(fields=('name')) Columns to list... Is it right? list(Model.select(lambda x: x.name)).. i think it returns full model...
Max
to_dict... to_list not in doc..
Alexey
https://docs.ponyorm.com/api_reference.html#Entity.to_dict
Max
yes... to_list?
Max
For example we want to get list of properties... [c.name for c in Model.select()]... Its really looks weird
Alexey
1) list(Model.select(lambda x: x.name)) 2) list comprehension, iterating on the result of to_dict
Alexey
select(c.name for c in Model)
Max
hm...
Alexander
Hi Maxim, > Who can provide simple example of group and join multiple tables with model Currently to grouping you need to use select(...) form of query: select((x.a, x.b, max(x.c), sum(x.d)) for x in MyEntity if x.e > y) will result to SELECT x.a, x,b, max(x.c), sum(x.d) FROM myentity x WHERE x.e > %(param1)s GROUP BY x.a, x.b
Alexander
There are two type of joins. The most convenient one is when you use explicitly defined entity relationships. The query select(s for s in Student if s.group.department.name == 'Department of Network Technologies') will lead to join of three tables - student, group and department The same query can be written using filter method with lambda: select(s for s in Student).filter(lambda s: s.group.department.name == 'Department of Network Technologies') Instead of select function you can use select method of Student entity: Student.select(lambda s: s.group.department.name == 'Department of Network Technologies')
Alexander
Currently the queries which use MyEntity.select(...) form restricted to return instances of MyEntity class only. If you want to return something else, you need to use select(...) function: select((s.name, s.age) for s in Student)
Alexander
In the future we probably will add method to query which will allow to specify result columns using lambda function, something like project method: Student.select(...).filter(...).project(lambda s: (s.name, s.age)) Currently we don't have such API, you need to use select((s.name, s.age) for s in Student) form or retrieve whole object and access necessary fields in Python
Alexander
The second type of join is explicit join without relationships. In order to make such join you need to write select(...) function with multiple for loops. For example, if you want to find different students with the same name you can write: select( (s1, s2) for s1 in Student for s2 in Student if s1.id != s2.id and s1.name == s2.name )
Alexander
If you want to convert query to list you can use slice: query = MyEntity.select(...) objects = query[:limit] Or, if you want to retrieve all objects, you can just wrap query to list() constructor: query = MyEntity.select(...) objects = list(query)
Max
Uh, thanks.... select(s for s in Student).filter(lambda s: s.group.departament.name = '') equivalent to Model.select(lamda s: s if s.group.deparment.name == 'name' )... smaller...?
Max
hm... understand... filter not in api... futures..
Henri
hm... understand... filter not in api... futures..
https://docs.ponyorm.com/api_reference.html#Query.filter
Max
=)
Budiyono
Hi Alex, just want to ask does Pony have feature to generate entities based on existing DB tables?
Alexander
Not yet. At this moment you can manually create entities which correspond to existing tables. It is possible to explicitly specify name of each table and column in model definition
Budiyono
ic ok because i got 100+ tables to migrate :D
Alexander
That's pretty big number :)
Lucky
Maybe you can still automate it. Most databases have a way to list existing tables and columns
Romet
you could maybe use django's inspect to generate django models for you
Romet
and then do some text processing to get pony-like models
Romet
obviously needs fine tuning by hand afterwards but way better than doing it all from scratch i suppose
Alexey
I think we should we need to offer such a database reflection tool @bsalim what database you currently use?
Budiyono
i m quite new to Pony :) mostly used Django ORM, thinking to switch to more lighter REST API framework with Pony ORM as main ORM
Matthew
I use flask + pony and find it works very well :)
Budiyono
i m currently looking at apistar project founded by DRF creator
Henri
I use Morepath and Pony and it works great!
Matthew
In [2]: select(u for u in User if u.id > 10) | select(u for u in User if 'gmail.com' in u.email_address) —------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-e4a413899bc9> in <module😠) —--> 1 select(u for u in User if u.id > 10) | select(u for u in User if 'gmail.com' in u.email_address) TypeError: unsupported operand type(s) for |: 'Query' and 'Query'
Matthew
Is composition (treating queries as sets) planned?
Matthew
maybe composition is the wrong word :)
Micaiah
Is there an ETA on Pony migrations?
Micaiah
I've been away for this chat for a hot minute so I might have missed it if they came out already
Lucky
Is there an ETA on Pony migrations?
No, as far as I know, there is only the migrants tool I wrote Github.com/luckydonald/pony_up
Matthew
Yeah those were just basic example queries
Matthew
It seems like it could be a good way of building up queries with a lot of nested ands and ors
Matthew
Dynamically
Matthew
For example in a loop doing query = query | select(...)
Matthew
Also with & and -
Henri
If someone is interested in some insides of the Morepath web-framework, Martijn Faassen gave just a great talk at PyMunich. https://www.youtube.com/watch?v=BUQhElQuxMI
stsouko
Hello! Is it possible on pony write a subqueries?
stsouko
currently I do this: sql_select = "x.bit_array %s '%s'::int2[]" % (operator, bit_set) sql_smlar = "smlar(x.bit_array, '%s'::int2[], 'N.i / (N.a + N.b - N.i)') as T" % bit_set select((x.molecule.id, x.id, raw_sql(sql_smlar)) for x in MoleculeStructure if raw_sql(sql_select)).order_by(raw_sql('T DESC')).limit(number * 2) but order operation is slow. and I want limiting it.
stsouko
SELECT "x"."molecule", "x"."id", smlar(x.bit_array, '{1409, 2306, ......}'::int2[], 'N.i / (N.a + N.b - N.i)') as T FROM "daniel"."molecule_structure" "x" WHERE x.bit_array %% '{1409, 2306, ......}'::int2[] ORDER BY T DESC LIMIT 20 replace this by: SELECT * FROM (SELECT "x"."molecule", "x"."id", smlar(x.bit_array, '{1409, 2306, ......}'::int2[], 'N.i / (N.a + N.b - N.i)') as T FROM "daniel"."molecule_structure" "x" WHERE x.bit_array %% '{1409, 2306, ......}'::int2[] LIMIT 20) as SQ ORDER BY T DESC
Alexander
Yes, it is possible to write the most popular types of subqueries with Pony. I'm not quite understand at this moment what type of subquery you want to use
stsouko
this is similarity search on arrays. I use SMLAR extension.