Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UndefinedFunctionError when not using the limit_raw_sql #704

Closed
SepehrBazyar opened this issue Jun 19, 2022 · 1 comment
Closed

UndefinedFunctionError when not using the limit_raw_sql #704

SepehrBazyar opened this issue Jun 19, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@SepehrBazyar
Copy link
Contributor

Describe the bug:
When I use limit & select_related together in QuerySet;
if I do not set limit_raw_sql to True, I get an Error.
And if I do;
The limit does not apply to the number of rows in the parent model.

To Reproduce:

# Models
class User(PrimaryKeyMixin, ormar.Model):
    """User Model Class to Implement Method for Operations of User Entity"""

    mobile: str = ormar.String(unique=True, index=True, max_length=10, pattern=MOBILE_PATTERN)
    password: str = ormar.String(max_length=128)
    level: str = ormar.String(max_length=1, choices=list(Level), default=Level.STAFF.value)
    email: Optional[EmailStr] = ormar.String(max_length=255, nullable=True, default=None)
    avatar: Optional[str] = ormar.String(max_length=255, nullable=True, default=None)
    fullname: Optional[str] = ormar.String(max_length=64, nullable=True, default=None)
    is_active: bool = ormar.Boolean(index=True, nullable=False, default=True)

    class Meta(MainMeta):
        orders_by = ["-is_active", "-level"]

class Task(PrimaryKeyMixin, ormar.Model):
    """Task Model Class to Implement Method for Operations of Task Entity"""

    name: str = ormar.String(max_length=64, nullalbe=False)
    description: Optional[str] = ormar.Text(nullable=True, default=None)
    start_date: Optional[date] = ormar.Date(nullable=True, default=None)
    end_date: Optional[date] = ormar.Date(nullable=True, default=None)
    is_halted: bool = ormar.Boolean(index=True, nullable=False, default=True)
    user: User = ormar.ForeignKey(to=User)

    class Meta(MainMeta):
        orders_by = ["-end_date", "-start_date"]
        constraints = [
            ormar.UniqueColumns("user", "name"),
        ]

# QuerySet
# (I have two users, each with two tasks)
>>> await User.objects.limit(2, limit_raw_sql=True).select_related(User.tasks).all()
>>> await User.objects.select_related(User.tasks).limit(2, limit_raw_sql=True).all()
[User({'id': UUID('0bc2ec3f-5756-4cd0-ae4a-bbe7b53aad23'), 'mobile': '9928917653', 'password': '$argon2id$v=19$m=65536,t=3,p=4$wNh7z7mXcu49x9j7P8eYUw$s1q1w/ZXKnO1tx4k6eqWgf22z/wYVlRDEntdWkVkiH4', 'level': '1', 'email': None, 'avatar': None, 'fullname': None, 'is_active': True, 'member': [], 'teamuser': None, 'tasks': [Task({'id': UUID('31a4be3d-055d-400d-b3b6-0b3b4379cdc2'), 'name': 'first', 'description': None, 'start_date': None, 'end_date': None, 'is_halted': True, 'user': <weakproxy at 0x0000022667234A40 to User at 0x0000022668178580>}), Task({'id': UUID('0922c607-6eaf-4ff6-8aa8-0766f49bc7b9'), 'name': 'two', 'description': None, 'start_date': None, 'end_date': None, 'is_halted': True, 'user': <weakproxy at 0x0000022667234A40 to User at 0x0000022668178580>})]})]

>>> await User.objects.limit(2).select_related(User.tasks).all()
>>> await User.objects.select_related(User.tasks).limit(2).all()
File ~/Desktop/MyProject/venv/Lib/site-packages/asyncpg/protocol/protocol.pyx:168, in prepare()

UndefinedFunctionError: function max(boolean) does not exist
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Versions:

  • Database backend: PostgreSQL
  • Python version: 3.10.1
  • ormar version: 0.11.1
  • pydantic version: 1.9.0
@SepehrBazyar SepehrBazyar added the bug Something isn't working label Jun 19, 2022
collerek added a commit that referenced this issue Jun 26, 2022
* fix schema with enum fields - issue #699

* fix drivers dependencies - make them optional

* fix command

* provide extras

* add bolean field to related model

* add test with select related and boolean

* new test case based on issue

* fix bool issue in postgres limit queries - issue #704

* fix coverage

* bump version and add release info
@collerek
Copy link
Owner

Thanks for catching that - should be fixed in 0.11.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants