From 8499913808ff63ae25b0ed2afe02522c04a5c37e Mon Sep 17 00:00:00 2001 From: collerek Date: Fri, 11 Dec 2020 13:30:01 +0100 Subject: [PATCH 1/3] fix issue #71 --- ormar/fields/foreign_key.py | 3 ++ tests/test_uuid_fks.py | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/test_uuid_fks.py diff --git a/ormar/fields/foreign_key.py b/ormar/fields/foreign_key.py index 72fd485..88e1928 100644 --- a/ormar/fields/foreign_key.py +++ b/ormar/fields/foreign_key.py @@ -1,3 +1,4 @@ +import uuid from typing import Any, List, Optional, TYPE_CHECKING, Type, Union import sqlalchemy @@ -123,6 +124,8 @@ class ForeignKeyField(BaseField): def _construct_model_from_pk( cls, value: Any, child: "Model", to_register: bool ) -> "Model": + if cls.to.pk_type() == uuid.UUID and isinstance(value, str): + value = uuid.UUID(value) if not isinstance(value, cls.to.pk_type()): raise RelationshipInstanceError( f"Relationship error - ForeignKey {cls.to.__name__} " diff --git a/tests/test_uuid_fks.py b/tests/test_uuid_fks.py new file mode 100644 index 0000000..29bef88 --- /dev/null +++ b/tests/test_uuid_fks.py @@ -0,0 +1,67 @@ +import uuid + +import databases +import pytest +import sqlalchemy +from sqlalchemy import create_engine + +import ormar +from tests.settings import DATABASE_URL + +metadata = sqlalchemy.MetaData() +db = databases.Database(DATABASE_URL) + + +class User(ormar.Model): + class Meta: + tablename = "user" + metadata = metadata + database = db + + id: uuid.UUID = ormar.UUID( + primary_key=True, default=uuid.uuid4, uuid_format="string" + ) + username = ormar.String(index=True, unique=True, null=False, max_length=255) + email = ormar.String(index=True, unique=True, nullable=False, max_length=255) + hashed_password = ormar.String(null=False, max_length=255) + is_active = ormar.Boolean(default=True, nullable=False) + is_superuser = ormar.Boolean(default=False, nullable=False) + + +class Token(ormar.Model): + class Meta: + tablename = "token" + metadata = metadata + database = db + + id = ormar.Integer(primary_key=True) + text = ormar.String(max_length=4, unique=True) + user = ormar.ForeignKey(User, related_name="tokens") + created_at = ormar.DateTime(server_default=sqlalchemy.func.now()) + + +@pytest.fixture(autouse=True, scope="module") +def create_test_database(): + engine = create_engine(DATABASE_URL) + metadata.create_all(engine) + yield + metadata.drop_all(engine) + + +@pytest.mark.asyncio +async def test_uuid_fk(): + async with db: + async with db.transaction(force_rollback=True): + user = await User.objects.create( + username="User1", + email="email@example.com", + hashed_password="^$EDACVS(&A&Y@2131aa", + is_active=True, + is_superuser=False, + ) + await Token.objects.create(text="AAAA", user=user) + page_size = 20 + page_num = 0 + await Token.objects.order_by("-created_at").limit(page_size).offset( + page_size * (page_num - 1) + ).all() From fd3bb53ba17fcfb33e5df8b8ff93e003d3c228bc Mon Sep 17 00:00:00 2001 From: collerek Date: Fri, 11 Dec 2020 13:32:22 +0100 Subject: [PATCH 2/3] bump version, update realease docs --- docs/releases.md | 7 ++++++- ormar/__init__.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/releases.md b/docs/releases.md index a09424b..b566795 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -1,3 +1,7 @@ +# 0.7.3 + +* Fix for setting fetching related model with UUDI pk, which is a string in raw (fix [#71][#71]) + # 0.7.2 * Fix for overwriting related models with pk only in `Model.update() with fields passed as parameters` (fix [#70][#70]) @@ -188,4 +192,5 @@ Add queryset level methods [#19]: https://github.com/collerek/ormar/issues/19 [#60]: https://github.com/collerek/ormar/issues/60 [#68]: https://github.com/collerek/ormar/issues/68 -[#70]: https://github.com/collerek/ormar/issues/70 \ No newline at end of file +[#70]: https://github.com/collerek/ormar/issues/70 +[#71]: https://github.com/collerek/ormar/issues/71 \ No newline at end of file diff --git a/ormar/__init__.py b/ormar/__init__.py index 0319847..4b9e747 100644 --- a/ormar/__init__.py +++ b/ormar/__init__.py @@ -44,7 +44,7 @@ class UndefinedType: # pragma no cover Undefined = UndefinedType() -__version__ = "0.7.2" +__version__ = "0.7.3" __all__ = [ "Integer", "BigInteger", From ef4b687957c395b8512f0a1984ce99f9e099ac1a Mon Sep 17 00:00:00 2001 From: collerek Date: Fri, 11 Dec 2020 13:35:08 +0100 Subject: [PATCH 3/3] fix negative offset in tests --- tests/test_uuid_fks.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/test_uuid_fks.py b/tests/test_uuid_fks.py index 29bef88..97f728a 100644 --- a/tests/test_uuid_fks.py +++ b/tests/test_uuid_fks.py @@ -60,8 +60,4 @@ async def test_uuid_fk(): is_superuser=False, ) await Token.objects.create(text="AAAA", user=user) - page_size = 20 - page_num = 0 - await Token.objects.order_by("-created_at").limit(page_size).offset( - page_size * (page_num - 1) - ).all() + await Token.objects.order_by("-created_at").all()