fix uuid column type in alembic autogeneration

This commit is contained in:
collerek
2020-11-30 07:50:47 +01:00
parent 98380a157d
commit 610fcf4944
4 changed files with 23 additions and 8 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
p38venv p38venv
alembic
alembic.ini
.idea .idea
.pytest_cache .pytest_cache
.mypy_cache .mypy_cache

View File

@ -167,6 +167,7 @@ target_metadata = metadata
# set your url here or import from settings # 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" URL = "sqlite:///test.db"
@ -183,10 +184,12 @@ def run_migrations_offline():
""" """
context.configure( context.configure(
url=URL url=URL,
target_metadata=target_metadata, target_metadata=target_metadata,
literal_binds=True, literal_binds=True,
dialect_opts={"paramstyle": "named"}, dialect_opts={"paramstyle": "named"},
# if you use UUID field set also this param
user_module_prefix='sa.'
) )
with context.begin_transaction(): with context.begin_transaction():
@ -205,7 +208,9 @@ def run_migrations_online():
with connectable.connect() as connection: with connectable.connect() as connection:
context.configure( context.configure(
connection=connection, 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(): 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): And you pass it into context like (both in online and offline):
```python ```python
context.configure( context.configure(
url=url, url=URL,
target_metadata=target_metadata, target_metadata=target_metadata,
literal_binds=True, literal_binds=True,
dialect_opts={"paramstyle": "named"}, dialect_opts={"paramstyle": "named"},
user_module_prefix='sa.',
include_object=include_object include_object=include_object
) )
``` ```

View File

@ -1,15 +1,15 @@
import uuid import uuid
from typing import Any, Optional, Union from typing import Any, Optional, Union
from sqlalchemy import CHAR
from sqlalchemy.engine.default import DefaultDialect from sqlalchemy.engine.default import DefaultDialect
from sqlalchemy.types import CHAR, TypeDecorator from sqlalchemy.types import TypeDecorator
class UUID(TypeDecorator): # pragma nocover class UUID(TypeDecorator): # pragma nocover
"""Platform-independent GUID type. """Platform-independent GUID type.
Uses Postgresql's UUID type, otherwise uses Uses CHAR(36) if in a string mode, otherwise uses CHAR(32), to store UUID.
CHAR(32), to store UUID.
""" """
@ -19,6 +19,11 @@ class UUID(TypeDecorator): # pragma nocover
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.uuid_format = uuid_format 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: def _cast_to_uuid(self, value: Union[str, int, bytes]) -> uuid.UUID:
if not isinstance(value, uuid.UUID): if not isinstance(value, uuid.UUID):
if isinstance(value, bytes): if isinstance(value, bytes):

View File

@ -42,7 +42,9 @@ class UUIDSample2(ormar.Model):
metadata = metadata metadata = metadata
database = database 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() test_text: str = ormar.Text()
@ -321,7 +323,7 @@ async def test_model_limit_with_filter():
await User.objects.create(name="Tom") await User.objects.create(name="Tom")
assert ( 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
) )