Anonymous
imagine having tens of them now
Anonymous
fastly becomes overwhelming ^^
Anonymous
how can i get the inserted row as a dictionary ?
Anonymous
because i wan to serialize it to json
Anonymous
i think non of the classes and query results are json serializable. how can i serialize them ?
Alexander
https://docs.ponyorm.org/api_reference.html#Entity.to_dict
Anonymous
https://docs.ponyorm.org/api_reference.html#Entity.to_dict
AttributeError: 'QueryResult' object has no attribute 'to_dict'
Alexander
It is a method of entity instance, not query result
Alexander
objects = select(s for s in Student)[:] data = [obj.to_dict() for obj in objects]
Anonymous
i'm getting an error : pony.orm.dbapiprovider.InternalError: (1054, "Unknown column 'front_history.system' in 'field list'") do you know from where it comes ?
Anonymous
got it after setting a reverse attribute front_history = Optional("FrontHistory") in System
Anonymous
It is a method of entity instance, not query result
take = (int(request.query['page']) * 10) offset = (int(request.query['page']) - 1) * 10 servers = select(server for server in Server)[offset:take] return web.Response(text=dumps({ "page": request.query['page'], "servers": dumps([obj.to_dict() for obj in servers]) }), status=200) do you see a problem on this code ? this is giving me Object of type UUID is not json serializable
Anonymous
it's not a query
Anonymous
Did you write query by hand?
i get this error from db.generate_mapping()
Alexander
i get this error from db.generate_mapping()
Maybe you created tables before you changed entity definition, and so you table front_history does not have expected column system
Anonymous
it has system_id
Anonymous
what should i change ? fk_name or column attribute ?
Anonymous
Maybe you created tables before you changed entity definition, and so you table front_history does not have expected column system
data = await request.json() server = Server(name=data['name'] if data['name'] else None, ip=data['ip'] if data['ip'] else None) commit() return web.Response(text=dumps({ "message": "سرور با موفقیت افزوده شد", "server": serialize(server.to_dict()) # "server": serialize(server.to_dict()) }), status=200, headers={ "Content-Type": "application/json" }) it is still not working 😭 that serizalize is function you gave
J J
how ? as a argument ?
json.dumps(x, default=serializer)
Anonymous
Anonymous
can i hide some fields from all dictironary results ?
Lucky
Hey, I am currently looking into async webservers, and Pony is currently not capable async afaik? Is it pony to let it generate the queries and then handle execution of them ourself?
Lucky
Like is there a function to turn orm.select(x for x in Foo if x.gnerf = 123).limit(42) to an SQL string?
Andrey
Like is there a function to turn orm.select(x for x in Foo if x.gnerf = 123).limit(42) to an SQL string?
See method get_sql of Query object: https://docs.ponyorm.org/api_reference.html#Query.get_sql
Anonymous
hi, i still have the problem with db.generate_mapping() : pony.orm.dbapiprovider.InternalError: (1054, "Unknown column 'front_history.system' in 'field list'")
Andrey
Anonymous
what does it mean exactly ?
Alexander
If you database is new and does not contain important data you can recreate the tables: db.generate_mapping(check_tables=False) db.drop_all_tables(with_all_data=True) db.create_tables()
Anonymous
but that would delete everything each time i run the program ?
Alexander
Do it just one time, then replace it with db.generate_mapping(check_tables=True)
Anonymous
i don't have set options in generate_mapping, should i ?
Anonymous
oh ok
Anonymous
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NULL\n)' at line 3
Anonymous
shouldn't it set JSON fields as text ?
Alexander
Pony does not support JSON with MariaDB yet
Anonymous
oh, is there a workaround to handle it ? like creating custom methods ?
Alexander
If you want to use JSON with PonyORM it is easier to use MySQL instead of MariaDB at this moment. Do you really need to use MariaDB?
Anonymous
it is MySQL
Alexander
According to the error, it is MariaDB
Anonymous
ye but MariaDB is a MySQL fork
Anonymous
so what works for MySQL should work for MariaDB
Alexander
Yes, but it has different JSON syntax
Anonymous
oh but i don't need a json field
Anonymous
it can be a text field
Anonymous
that get parsed as json after
Alexander
Then remove it at it will work ok
Anonymous
sth like that : color_roles = Optional(str) def get_color_roles(self): return json.loads(self.color_roles) def set_color_roles(self, color_roles): self.color_roles = json.dumps(color_roles)
Anonymous
also other question (sry for all the spam) : how to handle bigint ?
Alexander
If you don't need to write queries using some elements of JSON, this should work
Anonymous
yes i don't need it
Alexander
For bigint, add size=64 option to int attribute
Anonymous
oh ok, thanks
Anonymous
is it possible to add a db_session decorator on an asynchronous function ? or should i use the context manager ?
Anonymous
i'm getting pony.orm.core.TransactionError: db_session is required when working with the database altho i have db_session on my function
Alexander
What async framework are you using?
Anonymous
asyncio
Anonymous
according to pony doc it should work the same way as standard functions
Anonymous
it's also a d.py project, so i have other decorators on my functions, can it cause problems ?
Anonymous
@db_session @bot.event async def on_ready(): for guild in bot.guilds: if not Guild[guild.id]: Guild(id=guild.id)
Anonymous
example ^
Alexander
It turned out this functionality is broken, we need to fix it in documentation #todo It is better to not use Pony with async code at this moment Speaking in more details, you can, but you need to place db_session inside function as a context manager and never use async code inside db_session: @bot.event async def on_ready(): with db_session: # never do async calls here for guild in bot.guilds: if not Guild[guild.id]: Guild(id=guild.id) For asyncio you can try TortoiseORM or pewee-async https://github.com/tortoise/tortoise-orm https://github.com/05bit/peewee-async
Anonymous
i see, i'll have a look at those, thanks a lot ^^
Lucky
Why are all of those frameworks always tightly coupled to a model system as well. It would be so great if I could easily use the SQL code generation of Pony with an async SQL runtime
Nikolay
Are there plans for timezone-aware datetime support or should i make a new feature request issue? I'm looking for support of db-specific timezone-aware types for dbs that have them, and automatic conversion to/from timezone-naïve utc for dbs that don't. Though just the latter for all dbs would also work. Related issues #241 #434
Nikolay
I'm not sure how pluggable pony types system is, perhaps it can be a 3rd party plugin instead.
Alexander
Type system in Pony is not pluggable (at least at this moment), because proper type support requires not only conversions between SQL and Python, but also extensive support in query translator - any supported type can be used inside generator expressions. It is not easy to make it truly pluggable
Alexander
Regarding timezones, I think #434 probably covers it, so I don't think we need another issue
Lucky
What exactly do they need in the query translator?
Nvm. Dates need a BETWEEN and such.
Alexander
Yes. date.year, etc.
Alexander
date1 - date2
Alexander
So, for timezone-aware dates we need to consider, for example, should we allow to mix dates with and without timezones in a single expressions like date1 - date2, date1 < date2, etc, how to do implicit type conversions, etc.
Alexander
By the way, if I remember correctly, SQLAlchemy creator was strongly against using dates with timezones, because it creates more problem then it solves