[PonyORM-list] Always prefetch some related entities ?

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Sat Dec 13 13:00:43 UTC 2014


Hi, Tomislav!

> Is it possible to make an entity always prefetch some related entities?

At this moment it is not possible. You can read fl.fullPath before exiting
of db_session in order to force fetching of necessary related object.

When we using Pony in our current projects, we always construct JSON data
(or some other kind of response) withing the db session, so it is not
necessary to us to use the prefetch functionality at all. I think that the
approach when db session wraps entire request processing is the most easy
and efficient. But we can add the ability that you requested. It may be
done in two different way.

1) We can add prefetch option to attribute:

    directory = Required(lambda: Directory, prefetch=True)

2) We can add prefetch option to db_session:

    with db_session(prefetch=[ File.directory ]):
        fl = File[1]

Currently I prefer the second way more, because it allows to prefetch data
only if it is explicitly requested, whereas the first variant requires to
do prefetching in each db_session even if it is not necessary.


> It would be nice to be able to have 'calculated' attributes, which
wouldn't be saved and loaded from the database, but would be serialized
using to_dict()

We have a plan to add computed attributes, but our intention is different,
we want to use such attributes inside queries, as a way to simplify complex
expressions. That is, Pony should be able to translate such computed
attributes to SQL and inline them into the query.

Until computed attributes are implemented, you can solve your current
problem by overriding `to_dict` method and adding calculated values
manually:

    class File(database.Entity):
        filename = Required(unicode)
        directory = Required(lambda: Directory)

        @property
        def fullPath(self):
            return os.path.join(self.directory.path, self.filename)

        def to_dict(self, *args, **kwargs):
            result = super(File, self).to_dict(*args, **kwargs)
            result['fullPath'] = self.fullPath
            return result


Regards,
Alexander


On Sat, Dec 13, 2014 at 9:50 AM, Tomislav Tustonic <ttustonic at outlook.com>
wrote:
>
> Hello
>
> Is it possible to make an entity always prefetch some related entities?
> For example, something like this:
>
> class Directory(database.Entity):
>     path = Required(unicode, unique=True)
>     files = Set(lambda: File)
>
> class File(database.Entity):
>     filename = Required(unicode)
>     directory = Required(lambda: Directory)
>
>     @property
>     def fullPath(self):
>         return os.path.join(self.directory.path, self.filename)
>
> # case 1
> with db_session:
>     fl = File[1]
> print fl.fullPath
>
> # case 2
> with db_session:
>     fl = File.select(lambda f: f.id==1).prefetch(File.directory).get()
> print fl.fullPath
>
> Case 1 will not work, case 2 will. So, I'd like to be able to specify that
> getting File always loads Directory, so that fullPath is available outside
> db_session. Is it (or will it be) possible?
> Somewhat related to this, it would be nice to be able to have 'calculated'
> attributes, which wouldn't be saved and loaded from the database, but would
> be serialized using to_dict(). I don't think it's possible now.
>
> Thanks, Tom
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141213/f2262bf9/attachment.html>


More information about the ponyorm-list mailing list