Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async ForeignKeys

See original GitHub issue

At the moment if you have model

class Model(peewee.Model):
    other_model = peewee.ForeignKey(OrderModel)

using it like this


will trigger sync query.

I’ve already have this working so this issue is just to keep contribution in mind 😉

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:3
  • Comments:22 (18 by maintainers)

github_iconTop GitHub Comments

anjianshicommented, Dec 7, 2016

I have implemented get_related() method, maybe someone can use it.

import peewee_async

class ExtManager(peewee_async.Manager):
    async def get_related(self, instance, related_name, single_backref=False):
        return related instance for foreign key relationship
        return query for backref or return related instance if single_backref is True
        model_cls = type(instance)
        related_field = getattr(model_cls, related_name)

        if isinstance(related_field, ReverseRelationDescriptor):
            return await self._get_backrefs(instance, related_name, single_backref)
            return await self._get_foreign_key_target(instance, related_name)

    async def _get_foreign_key_target(self, instance, field_name):
        foreign_key_value = getattr(instance, field_name + "_id")

        model_cls = type(instance)
        foreign_key_field = getattr(model_cls, field_name)
        target_cls = foreign_key_field.rel_model
        target_field = foreign_key_field.to_field

        return await self.get(target_cls, target_field == foreign_key_value)

    async def _get_backrefs(self, instance, related_name, single_backref=False):
        query = getattr(instance, related_name)
        instances = await self.execute(query)

        if single_backref:
            for instance in instances:
                return instance
            raise query.model_class.DoesNotExist
            return instances
jackfischercommented, Oct 30, 2016

Thank you! I’m new to peewee and peewee async so this example is helpful. It might be a worthwhile addition to the docs. Thank you for all your work

Read more comments on GitHub >

github_iconTop Results From Across the Web

using async function into loop with nodejs? - Stack Overflow
I have to check if foreignKey exist, but I can't make a loop with my asynchronous query function function checkAllFK(tables, foreignKeys) ...
Read more >
Asynchronous I/O (asyncio) - SQLAlchemy 1.4 Documentation
Setting this to False is a way to detect only local-column based properties (i.e. scalar columns or many-to-one foreign keys) that would result ......
Read more >
Loose foreign keys - GitLab Docs
In GitLab, foreign keys are vital part of the database design process. ... this procedure to work, we must register which tables to...
Read more >
QueryInterface - Sequelize
async addColumn(table: string, key: string, attribute: object, ... Object specifying target table, column name to create foreign key constraint.
Read more >
Async query and save - EF6 - Microsoft Learn
Relationships, navigation properties, and foreign keys - EF6. Relationships, navigation properties, and foreign keys in Entity Framework 6 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found