Skip to content

Commit

Permalink
Fix enum schema (#715)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
collerek authored Jun 26, 2022
1 parent 9b6fa2e commit 6af92aa
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install poetry==1.1.11
poetry install
poetry install --extras "all"
env:
POETRY_VIRTUALENVS_CREATE: false
- name: Deploy
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install poetry==1.1.11
poetry install
poetry install --extras "all"
env:
POETRY_VIRTUALENVS_CREATE: false
- name: Run mysql
Expand Down
8 changes: 8 additions & 0 deletions docs/releases.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.11.2

## 🐛 Fixes

* Fix database drivers being required, while they should be optional [#713](https://github.com/collerek/ormar/issues/713)
* Fix boolean field problem in `limit` queries in postgres without `limit_raw_sql` flag [#704](https://github.com/collerek/ormar/issues/704)
* Fix enum_class spilling to schema causing errors in OpenAPI [#699](https://github.com/collerek/ormar/issues/699)

# 0.11.1

## 🐛 Fixes
Expand Down
6 changes: 4 additions & 2 deletions ormar/fields/model_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __new__(cls, *args: Any, **kwargs: Any) -> BaseField: # type: ignore
scale=kwargs.get("scale", None),
represent_as_str=kwargs.get("represent_as_base64_str", False),
)
enum_class = kwargs.get("enum_class", None)
enum_class = kwargs.pop("enum_class", None)
field_type = cls._type if enum_class is None else enum_class

namespace = dict(
Expand All @@ -170,7 +170,9 @@ def __new__(cls, *args: Any, **kwargs: Any) -> BaseField: # type: ignore
unique=kwargs.pop("unique", False),
pydantic_only=pydantic_only,
autoincrement=autoincrement,
column_type=cls.get_column_type(**kwargs, sql_nullable=sql_nullable),
column_type=cls.get_column_type(
**kwargs, sql_nullable=sql_nullable, enum_class=enum_class
),
choices=choices,
encrypt_secret=encrypt_secret,
encrypt_backend=encrypt_backend,
Expand Down
16 changes: 13 additions & 3 deletions ormar/queryset/actions/order_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def __init__(
def field_alias(self) -> str:
return self.target_model.get_column_alias(self.field_name)

@property
def is_postgres_bool(self) -> bool:
dialect = self.target_model.Meta.database._backend._dialect.name
field_type = self.target_model.Meta.model_fields[self.field_name].__type__
return dialect == "postgresql" and field_type == bool

def get_field_name_text(self) -> str:
"""
Escapes characters if it's required.
Expand All @@ -49,15 +55,19 @@ def get_field_name_text(self) -> str:
def get_min_or_max(self) -> sqlalchemy.sql.expression.TextClause:
"""
Used in limit sub queries where you need to use aggregated functions
in order to order by columns not included in group by.
in order to order by columns not included in group by. For postgres bool
field it's using bool_or function as aggregates does not work with this type
of columns.
:return: min or max function to order
:rtype: sqlalchemy.sql.elements.TextClause
"""
prefix = f"{self.table_prefix}_" if self.table_prefix else ""
if self.direction == "":
return text(f"min({prefix}{self.table}" f".{self.field_alias})")
return text(f"max({prefix}{self.table}" f".{self.field_alias}) desc")
function = "min" if not self.is_postgres_bool else "bool_or"
return text(f"{function}({prefix}{self.table}" f".{self.field_alias})")
function = "max" if not self.is_postgres_bool else "bool_or"
return text(f"{function}({prefix}{self.table}" f".{self.field_alias}) desc")

def get_text_clause(self) -> sqlalchemy.sql.expression.TextClause:
"""
Expand Down
Loading

0 comments on commit 6af92aa

Please sign in to comment.