Why is filtering by foreign keys fields not allowed? · ormar-orm/ormar · Discussion #845
Let's say we have two tables
class Author: id = ormar.Integer(primary_key=True) class Book ... author = ormar.ForeignKey(Author)
Then the following queries should be equivalent
Book.objects.get(author=5) Book.objects.get(Book.author==5)
However the second query raises the following exception
AttributeError: Cannot filter by Model, you need to provide model name
This is caused by self._check_field() inside the method FieldAccessor._select_operator. An alternative approach is to replace the last query by
Book.objects.get(Book.author.id==5)
but this will do a join query, which is different from the first query. My question is, why filtering by foreign keys fields is not allowed?