fastapi-practices/fastapi_best_architecture

UUID exception

downdawn opened this issue · 17 comments

Mysql5.7 uses the uuid type, and exceptions will occur during database migration.

default_factory=uuid4

In the new README, I state MySQL > 8.0, and in fact, I'm using all 8.0+ right now

The Sqlalchemy documentation does not specify the Mysql UUID type

https://docs.sqlalchemy.org/en/20/dialects/mysql.html#mysql-data-types
https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.Uuid
https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.UUID

But in Mysql < 8.0, there is no UUID type, you need to use CHAR(36) or BINARY(16) type storage

Yes, I will temporarily set the String first, and then change it to uuid to solve the problem.

uuid: Mapped[str] = mapped_column(UUID(as_uuid=True), init=False, default_factory=uuid4, unique=True)

Great work, are we going to change it to a UUID type?

No, this is not the best solution for version 5.7

Env:centos8、mysql8.0、python3.10.13

An exception occurs when the program is started:

No module named 'asyncmy.connection'

sqlalchemy/sqlalchemy#9308
long2ice/asyncmy#55

By installing the latest version 0.2.8, the program started successfully, but new problems are triggered

  File "asyncmy/converters.pyx", line 11, in asyncmy.converters.escape_item
    cpdef escape_item(val, str charset, mapping: dict = None):
  File "asyncmy/converters.pyx", line 26, in asyncmy.converters.escape_item
    val = encoder(val, mapping)
TypeError: Argument 'value' has incorrect type (expected str, got UUID)

So it's probably not a mysql version issue, it's something to do with asyncmy

So it's probably not a mysql version issue, it's something to do with asyncmy

No exception occurs in the win environment

Have you tried Ubuntu? I don't seem to have an anomaly in the docker environment

No, I am used to using centos system

I will check, but I may be late.

Sorry, I'm really struggling to implement Centos environment in my local environment, by the error message, it seems you can try Mapped[Uuid]/default_factory=custom_str_uuid4?

uuid: Mapped[Uuid] = mapped_column(String(50), init=False, default_factory=uuid4, unique=True)

Migration works, but still not when creating

It is best to ensure that the front and back types are consistent. If you use Uuid, you don't need String.

Mapped will automatically map the value type. String(xx) is to give the initial length of the Vachar type field, because of database requirements, and when you use the str type
Their relationship: Mapped[str] == String == Vachar

Well, we should have read the documentation here more thoroughly

https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html#mapped-column-derives-the-datatype-and-nullability-from-the-mapped-annotation

ideally:

uuid: Mapped[uuid.UUID] = mapped_column(init=False, default_factory=uuid4, unique=True)

#240

Hi,@downdawn

Do we need to make a best solution here? UUID type or mandatory UUID string?

Considering compatibility, it should be the latter