How can I use UUIDs in SQLAlchemy?

后端 未结 8 966
心在旅途
心在旅途 2020-12-07 10:58

Is there a way to define a column (primary key) as a UUID in SQLAlchemy if using PostgreSQL (Postgres)?

8条回答
  •  囚心锁ツ
    2020-12-07 11:32

    If you are happy with a 'String' column having UUID value, here goes a simple solution:

    def generate_uuid():
        return str(uuid.uuid4())
    
    class MyTable(Base):
        __tablename__ = 'my_table'
    
        uuid = Column(String, name="uuid", primary_key=True, default=generate_uuid)
    

提交回复
热议问题