diff --git a/.gitignore b/.gitignore index 035d4cb..b35dcd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ p38venv +alembic +alembic.ini .idea .pytest_cache .mypy_cache diff --git a/docs/models.md b/docs/models.md index 7ea6422..b4ac905 100644 --- a/docs/models.md +++ b/docs/models.md @@ -167,6 +167,7 @@ target_metadata = metadata # set your url here or import from settings +# note that by default url is in saved sqlachemy.url variable in alembic.ini file URL = "sqlite:///test.db" @@ -183,10 +184,12 @@ def run_migrations_offline(): """ context.configure( - url=URL + url=URL, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, + # if you use UUID field set also this param + user_module_prefix='sa.' ) with context.begin_transaction(): @@ -205,7 +208,9 @@ def run_migrations_online(): with connectable.connect() as connection: context.configure( connection=connection, - target_metadata=target_metadata + target_metadata=target_metadata, + # if you use UUID field set also this param + user_module_prefix='sa.' ) with context.begin_transaction(): @@ -237,10 +242,11 @@ def include_object(object, name, type_, reflected, compare_to): And you pass it into context like (both in online and offline): ```python context.configure( - url=url, + url=URL, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, + user_module_prefix='sa.', include_object=include_object ) ``` diff --git a/ormar/fields/sqlalchemy_uuid.py b/ormar/fields/sqlalchemy_uuid.py index ba2ecda..1fdffa2 100644 --- a/ormar/fields/sqlalchemy_uuid.py +++ b/ormar/fields/sqlalchemy_uuid.py @@ -1,15 +1,15 @@ import uuid from typing import Any, Optional, Union +from sqlalchemy import CHAR from sqlalchemy.engine.default import DefaultDialect -from sqlalchemy.types import CHAR, TypeDecorator +from sqlalchemy.types import TypeDecorator class UUID(TypeDecorator): # pragma nocover """Platform-independent GUID type. - Uses Postgresql's UUID type, otherwise uses - CHAR(32), to store UUID. + Uses CHAR(36) if in a string mode, otherwise uses CHAR(32), to store UUID. """ @@ -19,6 +19,11 @@ class UUID(TypeDecorator): # pragma nocover super().__init__(*args, **kwargs) self.uuid_format = uuid_format + def __repr__(self) -> str: + if self.uuid_format == "string": + return "CHAR(36)" + return "CHAR(32)" + def _cast_to_uuid(self, value: Union[str, int, bytes]) -> uuid.UUID: if not isinstance(value, uuid.UUID): if isinstance(value, bytes): diff --git a/tests/test_models.py b/tests/test_models.py index 3471b8a..3c9ffc8 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -42,7 +42,9 @@ class UUIDSample2(ormar.Model): metadata = metadata database = database - id: uuid.UUID = ormar.UUID(primary_key=True, default=uuid.uuid4, uuid_format='string') + id: uuid.UUID = ormar.UUID( + primary_key=True, default=uuid.uuid4, uuid_format="string" + ) test_text: str = ormar.Text() @@ -321,7 +323,7 @@ async def test_model_limit_with_filter(): await User.objects.create(name="Tom") assert ( - len(await User.objects.limit(2).filter(name__iexact="Tom").all()) == 2 + len(await User.objects.limit(2).filter(name__iexact="Tom").all()) == 2 )