pythonmysqlflaskormponyorm

Create Pony ORM entity without fetching related objects?


I recently started using Pony ORM, and I think it's awesome. Even though the api is well documented on the official website, I'm having a hard time working with relationships. In particular I want to insert a new entity which is part of a set. However I cannot seem to find a way to create the entity without fetching related objects first:

post = Post(
    #author = author_id, # Python complains about author's type, which is int and should be User
    author = User[author_id], # This works, but as I understand it actually fetches the user object
    #author = User(id=author_id), # This will try and (fail to) create a new record for the User table when I commit, am I right?
    # ...
)

In the end only the id value is inserted in the table, why should I fetch the entire object when I only need the id?

EDIT

I had a quick Peek at Pony ORM source code and using the primary key of the reverse entity should work, but even in that case we end up calling _get_by_raw_pkval_ which fetches the object either from the local cache or from the database, thus probably it's not possible.


Solution

  • It is a part of inner API and also not the way Pony assumes you use it, but you actually can use author = User._get_by_raw_pkval_((author_id,)) if you sure that you has those objects with these ids.