5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLAlchemyをMySQLで主キーをUUIDにするならSQLAlchemy-Utilsが便利

Posted at

概要

UUIDを主キーとして利用したい場合に、MySQLの場合、SQLAlchemy-Utilsってモジュールを利用するのが手っ取り早いかったのでメモ。

実装例

一部抜粋
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import Column

from sqlalchemy_utils import UUIDType

import uuid

Base = declarative_base()


class Hoge(Base):

  __tablename__ = 'hoges'
  
  id = Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4)

requirements.txt
sqlalchemy
sqlalchemy_utils

参考

SQLAlchemy-Utils
https://sqlalchemy-utils.readthedocs.io/en/latest/index.html

SQLAlchemy-Utils UUIDT
https://sqlalchemy-utils.readthedocs.io/en/latest/data_types.html#module-sqlalchemy_utils.types.uuid

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?