Mr.
Exist chat.type ?
Sameer
Hello, any progress on py3.12 support?
Radim
same here
Alexander
I'm going on vacation at the end of this week and I'm planning on spending a lot of time on Pony throughout August, including adding support for 3.12.
Sergey
👍
Denis
Has anyone build a pony app using nuitka? I have the following problems. For some reason, at least during execution, errors occur when calling len() and enumirate(). Has anyone encountered something similar?
Santosh
@metaprogrammer Can we have DB session in middleware in case of Fast API
Santosh
1. I need to have common db session for all the transactions I do in request method and also common authentication method which will be used as Depends on each request method
Alexander
Im not sure thats a good idea
If i remember correctly db_session is a wrapper around transaction itself and i believe it will also make cache go crazy
Santosh
Okay
Santosh
If I used multiple DB session (2) will the performance gets impacted ?
I am migrating to fastAPI but keeping pony as is, without replacing with sqlalchemy
Alexander
Santosh
It’s sync for now
Alexander
For synchronous code, it should be okay to use middleware and wrap an entire request handling into a single db_session. This was the main use case for which db_session was designed.
But with the current Pony implementation, db_session should never wrap async code. So you should not do:
async def my_middleware_function(request, call_next):
...
with db_session:
await call_next(request) # incorrect: async call inside db_session
...
As FastAPI middleware API uses async functions for middleware, it may be hard to achieve.
I'll try to overcome this Pony limitation this week and make it compatible with async code (i mean, not truly async, but at least work correctly with async code)
Santosh
Alexander
Btw I would not consider doing this at all.
Imo it's better to stick with some "clean architecture" and implement Repository layer working with database.
Like it doesnt feel natural to open database connection once you got a request
Alexander
Santosh
Santosh
Alexander
The performance impact should be minimal, if any, because Pony caches and reuses previous db connection.
But it is not always convenient, as you cannot easily use objects from the first db session in the second db session
Santosh
Timofei
Hello there! Is there any mixin in Pony ORM similar to Django models abstract=True? I want to create abstract class to inherit from, but don't want to create any tables for it
Alexander
Timofei
❗️Looking for contributors to write an open-source Python lib ala "pony-flask-admin" for one-line easy admin for pony ORM (in my project I've just implemented custom model views by hands, but that's not cool)
Vitaliy
Timofei
Timofei
Sounds kinda helpful
Vitaliy
I can share my snippets. In my app it is named as "expert mode". It is tightly integrated in project ecosystem but can be easily edited to use it standalone.
Timofei
Vitaliy
Sameer
Hello, any progress on py3.12 support?
Please let us know.
Alexander
Hello, we're in progress
All tests seem to pass for now
Mr.
time window != floodwait
Santosh
@metaprogrammer any plans for python3.12 support
Alexander
Update: It almost works, but the semantics of SQLite JSON handling have changed. Trying to workaround this before the release.
Gokul
hii
Sameer
@metaprogrammer is py3.12 released ?
Please let us know.
Radim
Alexander
I released Pony ORM 0.7.18 with Python 3.12 support and SQLite JSON fixes:
https://github.com/ponyorm/pony/releases/tag/v0.7.18
Please check that is works for you
Салют
I have some logic in init() method of model. But it's not invoked when instance is created with Model[n]. Why? I have to set inner model's fields everytime it is created from scratch or read from DB
Vitaliy
Alexander
Alexander
I have some logic in init() method of model. But it's not invoked when instance is created with Model[n]. Why? I have to set inner model's fields everytime it is created from scratch or read from DB
Hi! First, some background.
Pony tries to be as efficient as possible and avoids sending unnecessary queries to the database.
Consider the situation where you have an Order entity with a customer field. Suppose you do:
order = Order[123]
customer = order.customer
print(customer.id)
Here, we load two ORM objects, order and customer. How many SELECT queries do we need for this?
We need the first query to the Order table to fetch the order details, such as a customer id. But do we really need the second query to the Customer table?
The only attribute we retrieve from the customer object is its id. But we already know this ID from the order object, so it is not necessary to retrieve other fields for the Customer object from the database.
For that reason, at the line customer = order.customer Pony does not send a query to the database. Instead, Pony creates a partially-initialized customer object in memory that only contains customer.id. Such a partially initialized object that only contains a primary key is named seed in Pony internal terminology.
Later, if the code contains something like name = customer.name, Pony sees that it is necessary to actually load other customer's fields from the database. But, instead of issuing a query to load an individual row from the database, Pony sees that, for example, there are three Customer seeds (101, 123, 202) in the current db_session. Then Pony issues a query SELECT ... FROM Customer WHERE id in (101, 123, 202) to load multiple seeds in a single query. This way, Pony minimizes the number of database queries.
Alexander
I have some logic in init() method of model. But it's not invoked when instance is created with Model[n]. Why? I have to set inner model's fields everytime it is created from scratch or read from DB
Now, back to your question. The cases when you create a new object and when Pony loads an object from the database are very different.
When a new object is created, it is necessary to check that all data is correct and can be saved to the database. Sometimes, some fields need a type conversion.
When the object is loaded from the database, we already know that all fields are consistent and have correct data types. Also, it is possible that not all fields are loaded from the database at this moment.
For that reason, two different methods are necessary for these cases: when a new object is added to the database and when an existing object is loaded from the database into memory. However, it may be hard to provide user customization for the second case because if user-defined code starts touching unnecessary attributes, it can trigger unnecessary queries to the database.
Alexander
In general, the current version of Pony does not provide an official way to store user-defined "virtual" attributes in the object alongside the normal persistent attributes.
But you can define properties; in many cases, it is enough. For example:
class Person(db.Entity):
id = PrimaryKey(int)
first_name = Required(str)
last_name = Required(str)
@property
def full_name(self):
return self.first_name + ' ' + self.last_name
@full_name.setter
def full_name(self, value):
self.first_name, _, self.last_name = value.partition(' ')
If a property getter is a one-liner and does not contain Python-specific functions, you can even use them in SQL queries (although the resulted SQL may not be efficient, as it may be hard for the database to use indexes properly).
Alexander
If you really need to track the moment the object is created or retrieved from the database, you can override the _get_from_identity_map_ class method defined in EntityMeta. But at that moment, only primary key is known for the object.
Салют
Sameer
I am trying to update password but running into an issue -
Here is the function -
@db_session
def update_password(new_password):
sql_command = f"ALTER USER TEST IDENTIFIED BY '{new_password}'"
try:
logger.debug(f"sql_command fixed: {sql_command}")
db.execute(sql_command)
logger.debug("Password updated successfully.")
except Exception as e:
logger.error(f"Failed to update password: {e}")
Issue -Failed to update password: near \"USER\": syntax error
What should I fix here ?
Alexander
try this:
sql_command = "ALTER USER TEST IDENTIFIED BY $new_password"
Sameer
Alexander
What database do you use?
Sameer
Alexander
try this:
sql_command = f'ALTER USER TEST IDENTIFIED BY "{new_password}"'
Sameer
Alexander
except Exception as e:
code = getattr(e, "code", None)
logger.error(f"Failed to update password: {type(e).__name__}: {e}; {code}")
Sameer
Sameer
After db.bind(), is there a way to drop all the connections at the same time? As I need to run db.bind() again with new credentials but it gives me below error-
Database object was already bound to Oracle provider: Cannot initialize database
I added db.disconnect() but that’s not helpful.
Amo
Amo
I have about 20 to 30 entities
Amo
At least I need to assign the entities to class variables, not instance variables
Alexander
The current Pony API does not work well for type hints. As a workaround, you can try define dataclasses that mirror your entities, and specify this dataclasses in type hints.
In Pony 2.0 entities will be defined as a dataclass-like classes, untied from the db instance, and it should improve type support
Amo
OK thanks. When do you expect to release pony2?
Alexander
In a few years
Alexander
I hope to have some prototype version earlier
Amo
Hi,
I have a table with device name, data and timestamp, how can I get the last data for each device?
I raw sql I use select(... Where time in (select max(time) group by device name))
Sorry, writing form phone
Amo
Select * from Payload where timestamp in (select max(timestamp) from Payload where Id != '' group by ID)
Works for raw sql qry
Amo
Ah I see why Payload.select(lambda p: max(p. Timestamp)) does not work. It groups by ID not by device name. Any way to change this?
Lucky
Lucky
Lucky
Or rather .sort_by() for now,
https://docs.ponyorm.org/api_reference.html#Query.sort_by
Lucky
Artemiy
Artemiy
I extensively use all of the mentioned) But I do not expect db models to be integrated with datatypes and Pydantic. The main thing I need is PyWright, and it deduces Pony types.
Lucky
Alexander