

Luckydonald
15.02.2017
09:40:55
The problem with to_json is that it has weak possibilities in querying hierarchical data. We want to replace it with something like that:
select(g for g in UniversityGroup if g.graduate_year == 2018).extract("""
number,
major,
department {
number,
name,
faculty { name }
},
courses foreach(c) orderby(c.name) {
id, name, credits
}
students foreach(s) where(s.gpa > 3.5) orderby(s.name) limit(10) {
id, name, gpa
},
""")
With that you mean, that there are only the ids of other tables? And the plan is to actually include the subdata there?
Actually I like how it works with reference keys now.
The problem with to_json is that it has weak possibilities in querying hierarchical data. We want to replace it with something like that:
select(g for g in UniversityGroup if g.graduate_year == 2018).extract("""
number,
major,
department {
number,
name,
faculty { name }
},
courses foreach(c) orderby(c.name) {
id, name, credits
}
students foreach(s) where(s.gpa > 3.5) orderby(s.name) limit(10) {
id, name, gpa
},
""")
Also this looks neither like python nor like sql
I'm not sure if inventing your own query language for that is a good idea.


Alexander
15.02.2017
10:01:35
This based on our experience we got during developing the site fineartbiblio.com.
Sometimes it is useful to load complex hierarhical data following object relationships. For example, on art gallery site page you want to load 10 most popular artists, and for each of them load some specific fields, like name and year of birth, and also load several related collections - 10 most significant artworks, 5 most recent catalogs, and for each catalog load 3 illustrations, and for each artwork load list of provenance records, etc. As a result you will get some hierarchical JSON structure.
This requires some hierarchical query API. There are several ways how it may be done.
One way is to use some pure-Python constructions. We are experimented with this, and the result was disappointing. Either the API looks very ugly, or its insufficiently powerful when working with subcollections.
Another way is to use some existing hierarchical query API, like GraphQL. It is possible, but often looks overcomplicate ("cursors" and all that stuff) and too heavyweight.
The third way is to use custom query language with easy to understand syntax, which allows to embed arbitrary Python expressions for conditions. After experimenting with other approaches, I believe this is the most convenient way.
> Actually I like how it works with reference keys now.
So you are already use to_json? Can you tell a bit more how you use it? Do you manually process resulted data structure?
Actually, the data format of to_json result is pretty good. But it was intened to use with corresponding PonyJS library which uses that data to construct JavaScript obejcts which are equivalent to Python objects. Do you use unreleased version of PonyJS, or process data manually instead?

Google

Alexander
15.02.2017
10:06:24
What I dislike in to_json function is not the data format, but the lack of expressive power when it is necessary to recursively specify fields and subcollections which need to be retrieved

Luckydonald
15.02.2017
16:34:13
We don't use it already, but @mshekhter and I am looking to build an adapter to put sql data to an elasticsearch index. And the data structure returned looks perfect already. All left to to would be to map the datatypes to an elastic equivalent.
From what we found so far, we'd could use the json of the elements unchanged to push the data into the ES index.

Alexey
15.02.2017
18:15:06
@luckydonald did you try https://docs.ponyorm.com/api_reference.html#Entity.to_dict
may be this will work for you?

Luckydonald
15.02.2017
18:16:36
Can I specify that on a query restult too?
But probably yes.
In conjunction with database._get_schema_dict() it is basicly the same.

Alexander
15.02.2017
19:07:58
But why implement another lanuage if you could just write python?
We don't found expressive enough way to write hierarchical queries using pure Python, maybe you can suggest it. I think custom DSL with embedded Python fragments provides the good compromise between familiarity and expressivity.
But in the nearest perspective you can use to_dict(), maybe it fully suits you needs

Micaiah
15.02.2017
19:09:59
anything with flask or hug that i do uses to_dict() extensively

Rozen
17.02.2017
20:55:58
Hi, i'm having... like..
and odd error
i have this function
@db_session
def join(user, group):
jugador = Jugador.get(user=user,group=group)
res = NOTHING
if (not jugador):
user = User.get(id_user=user.id_user)
user = select( u for u in User if u.id_user == user.id_user)[:][0]
print(user.id_user)
jugador = Jugador(user=user,group=group)
juego = Juego.get(group=group)
if (jugador not in juego.jugadores):
juego.jugadores.add(jugador)
res = JOINED
return res
(sorry for not pastebin)

Google

Rozen
17.02.2017
20:56:48
user and group are preloaded objects from the table
when i print the id_user i get the number

Luckydonald
17.02.2017
20:57:14

Rozen
17.02.2017
20:57:39
but in this line jugador = Jugador(user=user,group=group)
i get ValueError: Attribute User.id_user is required

Micaiah
17.02.2017
21:03:08
Is the id_user set to PrimaryKey?

Rozen
17.02.2017
21:05:06
Yep

Micaiah
17.02.2017
21:08:38
Hmm, strange.

Alexander
17.02.2017
21:23:42
Can you provide full traceback?

Rozen
17.02.2017
21:33:47
In 5 mins

Alexander
17.02.2017
21:36:00
I think this is what happened:
1) Jugador class derived from User
2) User class has id_user column
3) This column does not have auto=True option
4) You create Jugador instance without specifying id_user value
You need to specify auto=True option when defining id_user attribute. Or you can omiss definition of id_user at all, and pony creates default id attribute with auto=True

Rozen
17.02.2017
21:49:30
oh ..
it was the 1 option
i don't know what happened but i wanted jugador nor to be a child class from User
well ... i think im a mess ?
Thanks!!

Alexander
17.02.2017
21:52:35
Sure!

Pavel
20.02.2017
14:19:55
Hi all.
I am new to relational dbs and have some questions.
I have existing project with mongodb and my own data access objects. I want to migrate it to postgresql using ponyorm. I already created db model and done with data migration.
The question is:
What is the best way to integrate pony in existing code?
Should I replace existing models?

Alexander
20.02.2017
14:42:45
I think it will be much easier to replace models than to use some wrappers

Google

Artur Rakhmatulin
20.02.2017
14:47:59
Hi!
If you use your customORM like CRUD and no more, I think you will have no problems if you replace your 'models'.
it would be better if you have tests

Pavel
20.02.2017
16:01:51
Thank you for replies.

Artur Rakhmatulin
22.02.2017
08:42:10
Hello!
Which rulles to work with to_json of Query objects?
[link to doc](https://docs.ponyorm.com/api_reference.html?highlight=json#Query.to_json)
when i try to corvert my Query object to json with to_json() i gex exception
The current user None which belongs to groups ['anybody'] has no rights to see the object ORDER[1088962] on the frontend
ORDER - is my entity
What is simple way to convert objects to json?

Micaiah
22.02.2017
08:46:16
to_json is getting replaced (i think). Will to_dict work?

Alexey
22.02.2017
08:46:21
`to_dict` works and will work
what we'd like to do is to offer a simple way of creating hierarchial JSON
Here is an example:
select(g for g in UniversityGroup if g.graduate_year == 2018).extract("""
number,
major,
department {
number,
name,
faculty { name }
},
courses foreach(c) orderby(c.name) {
id, name, credits
}
students foreach(s) where(s.gpa > 3.5) orderby(s.name) limit(10) {
id, name, gpa
},
""")
as the result you'll get JSON
currently you can use to_dict method and then convert it to JSON


Artur Rakhmatulin
22.02.2017
08:49:53

Micaiah
22.02.2017
08:50:31
select(g for g in UniversityGroup if g.graduate_year == 2018).extract("""
number,
major,
department {
number,
name,
faculty { name }
},
courses foreach(c) orderby(c.name) {
id, name, credits
}
students foreach(s) where(s.gpa > 3.5) orderby(s.name) limit(10) {
id, name, gpa
},
""")
Could there be a sort of Extract object that can be used in place of """..."""

Artur Rakhmatulin
22.02.2017
08:50:37

Alexey
22.02.2017
08:51:40

Artur Rakhmatulin
22.02.2017
08:55:22
Hmmm... what version include this fiture? 'Query' object has no attribute 'extract'

Alexey
22.02.2017
08:55:52
it is the next thing we are going to work on after we release the migration tool

Artur Rakhmatulin
22.02.2017
08:56:55
Ok :) Thank you

Alexey
22.02.2017
08:57:51
?

Pavel
22.02.2017
08:58:42
By the way, can you provide some examples or real project repos that use ponyorm?

Google

Alexey
22.02.2017
09:00:42
here https://libraries.io/pypi/pony/usage

Micaiah
22.02.2017
09:00:54

Alexey
22.02.2017
09:01:07

Pavel
22.02.2017
09:01:56

Luckydonald
22.02.2017
17:37:35
select(g for g in UniversityGroup if g.graduate_year == 2018).extract("""
number,
major,
department {
number,
name,
faculty { name }
},
courses foreach(c) orderby(c.name) {
id, name, credits
}
students foreach(s) where(s.gpa > 3.5) orderby(s.name) limit(10) {
id, name, gpa
},
""")
Pony ORM strives to be the most pythonic framework, but uses some completely new language just to generate json?
This strucks me as really a wrong in that scope.

Micaiah
22.02.2017
17:38:24
I agree, I've been trying to think of a better way to do it but, I haven't yet.
Some sort of schema object might do

Luckydonald
22.02.2017
17:40:39

Romet
22.02.2017
17:42:49
what on earth

Micaiah
22.02.2017
17:43:32
keeps track of if statements, i've seen it done if there is a lot of conditional logic dictating when the function loses scope

Romet
22.02.2017
17:44:04
you'd think indentation would be obvious enough

Micaiah
22.02.2017
17:44:55
there might also be a plug in for an IDE which benefits from those kind of comments

Luckydonald
22.02.2017
17:45:39
I just started doing that, and figured it helps

Romet
22.02.2017
17:46:02
I'm not really being nitpicky, I just found it sort of amusing since I've never seen it before

Luckydonald
22.02.2017
17:51:08
select(g for g in UniversityGroup if g.graduate_year == 2018).extract("""
number,
major,
department {
number,
name,
faculty { name }
},
courses foreach(c) orderby(c.name) {
id, name, credits
}
students foreach(s) where(s.gpa > 3.5) orderby(s.name) limit(10) {
id, name, gpa
},
""")
return {
"number": g.number,
"major": g.major,
"department": {
"number": g.department.number,
"faculty": g.faculty.name,
},
"courses": [
{"id": c.id, "name": c.name, "credits": c.credits}
for c in g.courses.order_by(c.name)
],
"students": [
{"id": s.id, "name": s.name, "gpa": s.credits}
for s in g.students.order_by(s.name).limit(10) if s.gpa > 3.5
]
}

Romet
22.02.2017
17:51:46
telegram really needs to expand chat bubbles to full width..

Luckydonald
22.02.2017
17:52:12

Google

Romet
22.02.2017
17:53:05
definitely

Micaiah
22.02.2017
17:53:23
syntax highlighting would be great
There is already some markdown support, idk why they don't allow
```python
to work

Luckydonald
22.02.2017
18:00:06
return {
"number": g.number,
"major": g.major,
"department": {
"number": g.department.number,
"faculty": g.faculty.name,
},
"courses": [
{"id": c.id, "name": c.name, "credits": c.credits}
for c in g.courses.order_by(c.name)
],
"students": [
{"id": s.id, "name": s.name, "gpa": s.credits}
for s in g.students.order_by(s.name).limit(10) if s.gpa > 3.5
]
}
Getting back to topic :D
I really don't think you guys should invent a new language just for that.
What are the reasons not to use python?


stsouko
22.02.2017
18:50:56
next(dict(model=m.id, name=m.name, description=m.description, type=m.type,
destinations=[dict(host=x.host, port=x.port, password=x.password, name=x.name)
for x in m.destinations])
for m in select(m for m in Model if m.model_type == _type.value))
for JSONize I think this good
pure python.


Luckydonald
23.02.2017
00:48:26
Well... you asked about get, and select is entirely different story. Pony should translate select generator code to SQL, and cannot translate your function, because it doesn't know what equivalent SQL should be. I don't know it too :)
Also, the function that you wrote expects keyword arguments, not positional ones as you use inside the query.
1) I suggest you to write selects explicitly:
games = select(g for g in Game if (g.name == name or name is None) and (g.version == version or version is None))
2) You can also build query incrementally:
query = select(g for g in Game)
if name is not None:
query = query.filter(lambda g: g.name == name)
if version is not None:
query = query.filter(lambda g: g.version == version)
3) Probably even something like that should work:
def ignore_none(query, **kwargs):
for key, value in kwargs.items():
if value is not None:
query = query.filter(lambda x: getattr(x, key) == value)
return query
query = select(g for g in Game)
query = ignore_none(query, name='X', version='Y')
I didn't test it. Theoretically it should work, but there is an open issue 223, which describes a bug with getattr which is not fixed yet. You may follow approach (2) for safety
This .filter(lambda args: bool) in 2) is awesome.
packs = orm.select(p for p in Pack if not only_published or p.published)
Is now
packs = orm.select(p for p in Pack)
if only_published:
packs = packs.filter(lambda p : p.published)
# end if


Stavros
27.02.2017
19:22:08
Is there a way to create tables using a json file that handles field declaration?

Alexander
27.02.2017
21:40:43
In principle, it is possible to create entity classes dynamically. Simplified example:
from pony.orm import *
from pony.orm import core
db = Database()
def define_person_entity(db):
attrs = []
attrs.append(('name', Required(str)))
attrs.append(('age', Required(int)))
Person = core.EntityMeta('Person', (db.Entity,), dict(attrs))
define_person_entity(db)
db.bind('sqlite', ':memory:')
sql_debug(True)
db.generate_mapping(create_tables=True)
with db_session:
p1 = db.Person(name='John', age=18)
p2 = db.Person(name='Mike', age=20)
persons = select(p for p in db.Person)[:]
You can use the same idea to generate entity definitions based on information from some configuration file

Micaiah
28.02.2017
05:15:30
This schema library looks like it would be useful to JSON based Pony work, and has a similar API https://pypi.python.org/pypi/schema

Stavros
28.02.2017
06:19:44
Ty guys and awesome :)

Serge
28.02.2017
12:08:21
> from pony.orm import *
> from pony.orm import core
O_O?