Closed IUChimes closed 1 year ago
Hey @IUChimes,
Did you encounter an issue with the following syntax:
user = await Users.get(Users.email == email)
Typically, you should not be explicitly creating DataBaseModelCondition
s, as these are generated by using operators with DataBaseModel
attributes, or other DataBaseModelAttribute
comparison methods:
# this syntax should create a DataBaseModelCondition
Users.email == email
Good catch, I missed that part where I could pass it simply like that. Thank you. This seems less of a problem now with this "workaround". I guess it would be nice to have it work the same way as the primary key column since it is unique, but it may cost too many hours for only philosophical improvements.
Feel free to close this, as I am satisfied with your provided solution.
Function of interest (core.py):
Function is used with a code similar to the one below (Taken from easyauth package)
So given a database again taken from easyauth package with small modifications:
Users.get
is only able to retrieve elements that havePRIMARY KEY
, to ensure that it is unique. So in this case I have addedUNIQUE INDEX
on columnemail
, which should also ensure that all emails will be unique in the same way as username. But function mentioned above only checks if searchable column hasPRIMARY KEY
.From my short analysis, I could not find a way to include columns that have
UNIQUE INDEX
ascls.__metadata__.tables[cls.__name__]
does not hold information about indices.So my quick and dirty solution is to allow retrieval of elements using any column.
Current implementation allows the use of
DataBaseModelCondition
. This would be the cleanest approach without requiring any internal changes.Although it feels weird to allow such string interpolation with
f-string
instead of using integrated DB mechanisms. (More info on f-string vulnerability https://github.com/arogozhnikov/python3_with_pleasure/issues/3)I would suggest to include
Unique Index
data in table metadata, and improve the function to filter columns that have eitherprimary key
orunique index
. Also to exposeUnique (by Index)
in a similar way asPrimaryKey
to use in applications. This could also be used to allow login using email in easyauth application.