Closed croshchupkin closed 5 years ago
After having stepped through the code for a bit, it seems that pypika constructs the following SQL query: UPDATE "contact" SET "phone_no"='1111',"type"=ContactTypeEnum.other
I assume that the query will work fine if I do int(ContactTypeEnum.other) - but I just find it strange that the entity is created without issue while the update has problems.
After digging around some more, I think this is most likely not a bug, but a "feature" of Enum's default __str__
. This can be tweaked by creating the enum like so:
class ContactTypeEnum(IntEnum):
home = 1
work = 2
other = 3
def __str__(self):
return str(self._value_)
For REPL readability purposes, in the end, it's probably better to just explicitly cast to int, I suppose...
Creates are done using parametrized queries, updates/selects are not yet. related to #81
Could you add this as some failing tests that are marked with @test.expectedFailure
that does insert/update and filter on an enumfield?
The same feature also blocks dealing with binary blobs.
Ok, I'll try to do this later today.
Some Travis builds are failing for the pull request, but I'm not exactly sure why...
I had a look, and it is because the linter complained about import order:
flake8 tortoise/ examples/ setup.py conftest.py
tortoise/tests/test_update.py:2:1: I001 isort found an import in the wrong position
Makefile:27: recipe for target 'check' failed
make: *** [check] Error 1
I tried to document some basic guidelines here: https://tortoise-orm.readthedocs.io/en/latest/CONTRIBUTING.html#style
But basically all it means is that an import at line 2 of test_update.py isn't according to the isort (https://github.com/timothycrosley/isort) spec.
Basically run isort tortoise/tests/test_update.py
or make style
.
It will update imports to be grouped and sorted:
from tortoise.tests.testmodels import Event, Tournament, Contact, ContactTypeEnum
to
from tortoise.tests.testmodels import Contact, ContactTypeEnum, Event, Tournament
@croshchupkin You are trying to save something other than INT to IntField
In [2]: type(ContactTypeEnum.work)
Out[2]: <enum 'ContactTypeEnum'>
In [3]: type(ContactTypeEnum.work.value)
Out[3]: int
It's normal behavior. The only change that is required is to provide a readable exception.
P.S. You can use something like an EnumField used in the test suite:
class Contact(Model):
id = fields.IntField(pk=True)
type = EnumField(enum_type=ContactTypeEnum, default=ContactTypeEnum.other)
Basically, subj. To repro, please use the following code:
Then run the script:
I get the following exception: tortoise.exceptions.OperationalError: no such column: ContactTypeEnum.other. But before the update, the new model instance was saved successfully.