How can I use UUIDs in SQLAlchemy?


Question

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

1
60
11/13/2013 7:50:51 AM

The sqlalchemy postgres dialect supports UUID columns. This is easy (and the question is specifically postgres) -- I don't understand why the other answers are all so complicated.

Here is an example:

from sqlalchemy.dialects.postgresql import UUID
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Foo(db.Model):
    id = Column(Integer, primary_key=True)
    uuid = Column(UUID(as_uuid=True), unique=True, nullable=False)
69
7/29/2018 11:23:17 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon