Genesis Shards Community
Genesis Shards Community
pr eso use el Float
Alexander
previous value of _ingresos should be Decimal too
Alexander
it is not possible to mix floats and decimals
Genesis Shards Community
Alexander
I don't know how you initially construct it
Genesis Shards Community
Alexander
should be _ingresos = Decimal('0.0')
Alexander
0.00 is float, not decimal
Genesis Shards Community
wauuuuu!! thanks!! @metaprogrammer
Alexander
Sure
Genesis Shards Community
problem solved, thanks for your help
Genesis Shards Community
Genesis Shards Community
Genesis Shards Community
generated without errors
Anonymous
Hi folks. I'm looking for a way to build the json path used in the select statement for JSON structure queries (as shown here https://docs.ponyorm.org/json.html#querying-json-structures) in a dynamic way. I tried passing a custom subgenerator to the select function but this seems not to work because the select statement doesn't allow a lambda function with any more then a single parameter
Anonymous
Is there any other way how I could build the path used for quering dynamicly? (nested JSON structures)
Alexander
Hi, what do you mean by building path in a dynamic way? Can you give an example?
Anonymous
quick example of what I'm trying to do. Let's take the example from the link I mentioned: Product.select(lambda p: p.info['display']['size'] > 5) —> I'd like to have "display" and "size" and possibly more to be variables
Anonymous
I'm aware that I can use variables for the slection but what I want is to pass an array, containing multiple strings (variables of arbitrary length) and then each of them is used as key for the select
Alexander
An array of arbitrary length?
Anonymous
let me write a quick pseudo function
Alexander
Also, what database do you use?
Anonymous
sqlite so variables should work in the select
Anonymous
def get_custom_data(path: List[str], value:str):
return Product.select(lambda p: p.info[path[0]][path[1]]...[path[n]] is value)
get_custom_data(path=["my", "custom", "json", "path", "to", "select"], value="something")
Anonymous
if the path is of fixed length then this is easy. but I struggle with variable length...
Anonymous
I tried using a subgenerator that would build the path dynamicly and passing it into the lambda function of the select(), like this:
Anonymous
def deref(data, keys):
yield from deref(data[keys[0]], keys[1:]) if keys else (yield data)
Model.select(lambda x, y, z: next(deref(data=x, keys=y)) == z)
Anonymous
but this doesn't work. getting the error: Lambda query requires exactly one parameter name, like Model.select(lambda x: ...). Got: 3 parameters.
Anonymous
the reason for this seems to be here: https://github.com/ponyorm/pony/blob/b4d3da69d2fbd18c870db204d7de12a06e0b2328/pony/orm/core.py#L4353
Anonymous
Not sure if it's clear what I'm trying to do here... let me know if I should try to explain it in a different way.
Alexander
Pony json expressions does not support pathes with a variable length. So you need to use a bit of raw sql here:
Alexander
def find_product(path, value):
sql_expr = '''CAST(py_json_extract("p"."info", '%s') as text)''' % path.replace('$', '$$')
return Product.select(lambda p: raw_sql(sql_expr) == value)
path = "$.key1.key2[10][20]"
result = find_product(path, 'some_value')
You need to construct path argument dynamically as a string
Anonymous
yes that's what I saw is used internally here https://github.com/ponyorm/pony/blob/a8b0a5ca5cd8fab48b27c97a65aee211952ae2ac/pony/orm/dbproviders/sqlite.py#L546
Alexander
or, a bit simpler:
def find_product(path, value):
sql_expr = '''CAST(py_json_extract("p"."info", '$$.%s') as text)''' % path
return Product.select(lambda p: raw_sql(sql_expr) == value)
path = "key1.key2[10][20]"
result = find_product(path, 'some_value')
Anonymous
I'll try that, thanks!
Anonymous
Works perfectly! Thanks alot!
Alexander
Sure
Anonymous
Is there a way to make sure some values in a JSON attribute are unique? Currently when creating the entity I'm getting the relevant values from the JSON data and am saving them as a seperate key with unique set to true.
Alexander
In some databases it is possible to construct indexes (including unique indexes) based on expressions (including JSON path expressions)
At this moment Pony does not support support such indexes
I think it is easier in your case just to define separate unique column.
If you have such a column, maybe it is not necessary to have the same data in json field?
Alexander
This is how you can define expression-based index in SQLite
https://www.sqlite.org/expridx.html
Ptobably at this moment you can't use py_json_extract in such expression
Anonymous
true but in my case I just want to dump the json data if the query does match so it makes sense to have the data duplicated since that field should be included as well
Anonymous
cool, thanks for the info. totatly works for me to have that column duplicated. I was just wondering if it's possible. haven't seen anything about it while browing the docs. would have been a "nice to have" but as I said: duplicated column is fine.
Anonymous
Is there any recommanded way to pass the pony.orm.Database()? I'm doing the database init in my main.py but then other modules/classes need to access it. Currently I just save it in an module level constant _DB and import it where needed (e.g. Model classes). A more elegant way might be to package it inside a singleton but maybe there is another best practise? Also I'm not sure what the implications of having a single _DB constant are considering it will be accessed by multiple workers of my WSGI application. Is there anything to look out for? Do I need seperate connections for every worker / thread or is that handled within the Database() instance?
Alexander
There are several ways, but all of them consists of creating db = Database() on module level and then import it like
from app import db or from models import db.
Anonymous
I see. And then connections are handled inside Database() in a thread-safe manner I guess?
Anonymous
So there's really nothing I need to handle? I remember in pewee there have been WSGI server impl. specific session handling decorators and stuff
Anonymous
I guess this answers my question: https://docs.ponyorm.org/api_reference.html?highlight=thread#database-class
Anonymous
"The Database object manages database connections using a connection pool. It is thread safe and can be shared between all threads in your application. "
Anonymous
Awesome. So it's all handled when using the db_session decorator or context manager
Anonymous
Finally a Python ORM that doesn't suck.
Christian
.😁
Anonymous
Is there a way to just return all entities of a model? Can't figure out what the lambda equivalent to "Select * from Product" would be...
Anonymous
orm.select(p for p in Product)[:] works thought. Still dunno how the lambda would look like
Alexander
you can also do
Product.select()[:]
Anonymous
I thought I tried that and it failed with some exception. But now that you mentioned it I tried it again and it worked O_o. I guess I'm getting tired.
Genesis Shards Community
hi, good morning, hellp with its error: pony.orm.sqltranslation.IncomparableTypesError: Incomparable types 'date' and 'str' in expression
Genesis Shards Community
Genesis Shards Community
Genesis Shards Community
Genesis Shards Community
Anatoliy
you try put string in datetime
Anatoliy
replace data['fetcha'] to datetime.strptime(data['fetcha'],'%Y-%m-%d')
Genesis Shards Community
thanks!!
Anonymous
Hi guys!! I cannot figure out how can stablish a 1-many relatión with a composite key. I tried with reverse/columns but... With no success
Anonymous
class Parent(db.Entity):
pk_1 = Required(int, size=64)
pk_2 = Required(int, size=64)
config = Optional(Json)
PrimaryKey(pk_1, pk_2)
watermarks = Set('Child')
class Child(db.Entity):
pk_1 = Required(Parent, reverse='watermarks')
pk_2 = Required(Parent, reverse='watermarks')
pk_3 = Required(int, size=64)
config = Optional(Json)
PrimaryKey(pk_1, pk_2, pk_3)
Alexander
class Parent(db.Entity):
pk_1 = Required(int, size=64)
pk_2 = Required(int, size=64)
config = Optional(Json)
PrimaryKey(pk_1, pk_2)
watermarks = Set('Child')
class Child(db.Entity):
parent = Required(Parent, reverse='watermarks')
pk_3 = Required(int, size=64)
config = Optional(Json)
PrimaryKey(parent, pk_3)
The physical primary key of Child table will consist of 3 columns (parent_pk_1, parent_pk_2 and pk_3)
Drop
Hi
Any news about migrations?
Alexander
It’s almost done
Drop
😳
great)
Alexander
We need to implement downgrade, test it and write documentation
Alexander
It's 90% ready. Writing tests, finding some bugs, fixing it
Alexander
:)
Alexander
Anonymous
Thx Alexander
Alexander
Hehehe.
Alexander
It’s not a main goal now but in future we want to support it.
Main problem is transactions are kinda anti-async
Anonymous
Any quick way to convert TrackedDict returned by the JSON Data Type into a normal dict? I see that json.dumps works on TrackedDict but I don't want to convert it into JSON just to convert it back into a editable dict...