monobot / asyncorm

Fully Async ORM inspired in django's

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't work on the table with a uuid type as primary key

jiamo opened this issue · comments

commented
  • asyncorm version:
  • Python version: >3.5
  • Operating System: nix

Description

database : uuid type with column name 'id' as primary key, have some error to handle

What I Did

case 1:

class Users(models.Model):
    table_name = "users"
    id = models.Uuid4Field(uuid_type='v1', db_column='id')

with this query

    user_query = Users.objects.filter(
        id=uuid.UUID(user_id, version=1))

    print(user_query.query)
    users = []
    async for user in user_query:
        users.append(user)
    print(f"results:{users}")

have this error

2017-10-20 00:57:20,112 [17174] ERROR sanic(104) Traceback (most recent call last):
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/sanic/app.py", line 503, in handle_request
    response = await response
  File "/mnt/d/develop/test_fm/papayafm/api/v1/view.py", line 26, in user
    user_info = await op_user.user_orm_info(request, user_id)
  File "/mnt/d/develop/test_fm/papayafm/api/common/operation/op_user.py", line 18, in user_orm_info
    user_query = Users.objects.filter(id=uuid.UUID(user_id, version=1))
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/manager/managers.py", line 378, in filter
    filters = self.calc_filters(kwargs, exclude)
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/manager/managers.py", line 367, in calc_filters
    v = field.sanitize_data(v)
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/models/fields.py", line 151, in sanitize_data
    self.validate(value)
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/models/fields.py", line 139, in validate
    value, self.__class__.__name__
asyncorm.exceptions.FieldError: 7a8ef3bc-b4da-11e7-8e7a-f0def1b6e8a0 is a wrong datatype for field PkField

case 2:
I find some check related the Pkfield so I change the model

class Users(models.Model):
    table_name = "users"
    uid = models.Uuid4Field(uuid_type='v1')

with this quey

async def user_orm_info(request, user_id):
    # This too ugly why not sql session
    user_query = Users.objects.filter(
        uid=uuid.UUID(user_id, version=1))

    print(user_query.query)
    users = []
    async for user in user_query:
        users.append(user)
    print(f"results:{users}")

This time error :

2017-10-20 01:15:19,778 [17650] ERROR sanic(104) Traceback (most recent call last):
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/sanic/app.py", line 503, in handle_request
    response = await response
  File "/mnt/d/develop/test_fm/papayafm/api/v1/view.py", line 26, in user
    user_info = await op_user.user_orm_info(request, user_id)
  File "/mnt/d/develop/test_fm/papayafm/api/common/operation/op_user.py", line 23, in user_orm_info
    async for user in user_query:
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/manager/managers.py", line 519, in __anext__
    async for rec in self._cursor:
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/database/db_manager.py", line 40, in __anext__
    self._results = await self.get_results()
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncorm/database/db_manager.py", line 18, in get_results
    self._cursor = await self._conn.cursor(self._query)
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncpg/cursor.py", line 196, in _init
    self._query, timeout, named=True)
  File "/home/jiamo/anaconda3/lib/python3.6/site-packages/asyncpg/connection.py", line 285, in _get_statement
    statement = await self._protocol.prepare(stmt_name, query, timeout)
  File "asyncpg/protocol/protocol.pyx", line 163, in prepare (asyncpg/protocol/protocol.c:66355)
asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "a8ef3bc"

So I print the print(user_query.query)

Got some thing like:

[{'action': 'db__select_all', 'select': '*', 'table_name': 'Users', 'ordering': None, 'join': ''}, {'action': 'db__where', 'condition': 'users.id = 7a8ef3bc-b4da-11e7-8e7a-f0def1b6e8a0'}]

It seem like 'users.id = 7a8ef3bc-b4da-11e7-8e7a-f0def1b6e8a0' the uuid need be round by ''.

Right now asyncorm only accepts serial primary keys, I will add other primary keys and index fields as soon as possible.
Sorry for the inconvenience and late response.

Just did some changes so any field can be indexed, will allow other primary keys soonish.

Push pending because I have intermittent internet access