Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UUID Column and Type decorator #11

Closed
mehcode opened this issue Jul 5, 2013 · 6 comments
Closed

UUID Column and Type decorator #11

mehcode opened this issue Jul 5, 2013 · 6 comments

Comments

@mehcode
Copy link
Collaborator

mehcode commented Jul 5, 2013

I'm thinking of the following primary features:

  • A type decorator that maps a python uuid.UUID to a UUID type in postgres, mysql / mariadb (new versions), etc. and to a BINARY(16) in other dialects. This could also use the coercion listener to auto-convert strings, etc. to uuid.UUID.
  • A column that wraps sa.Column and adds various arguments that map to arguments for uuid.UUID and an auto=True|False flag that will make using as a primary_key easier.
import uuid
import sqlalchemy as sa
from sqlalchemy_utils.columns import UUIDColumn
from sqlalchemy_utils.types import UUIDType

class ModelWithColumn(Base):
    id = UUIDColumn(version=4, primary_key=True, auto=True)

class ModelWithType(Base):
    id = sa.Column(
        UUIDType, 
        primary_key=True, 
        default=uuid.uuid4)
@kvesteri
Copy link
Owner

kvesteri commented Jul 6, 2013

Good job with this one too!

Some suggestions:

  1. As with Password column #12, I think we should not override basic column syntax (atleast not in this case). We can just pass the uuid type parameters for the type object constructor:
import uuid
import sqlalchemy as sa
from sqlalchemy_utils.types import UUIDType

class ModelWithType(Base):
    id = sa.Column(
        UUIDType(version=4), 
        primary_key=True, 
    )
  1. Some users may want to store the UUID as VARCHAR(32), this should also be configuration option.
  2. MSSQL should use GUID datatype (looking at mssql dialect of sqlalchemy it seems it does not support this datatype atm). Maybe we could add this later?

The implementation could closely resemble this: http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#backend-agnostic-guid-type

However let's use the name UUIDType instead of GUID. GUID is just a name for Microsoft's own datatype that implements UUID spec.

@mehcode
Copy link
Collaborator Author

mehcode commented Jul 9, 2013

My only reservation with making this a type is I can't quite wrap my head around how to make UUIDType(auto=True) work as the default argument is on sa.Column(). Would we just return a new uuid from the bind method on the type if null is passed?

@kvesteri
Copy link
Owner

kvesteri commented Jul 9, 2013

What do we want to achieve with auto=True? Is it sufficient if the uuid values are assigned at pre-flush phase? If so I would just make another listener to SA-Utils which would assign uuids to all transient object's UUIDType auto columns.

Something like this:

def auto_assign_uuids(session, flush_context, instances):
    for obj in session.new:
        for prop in obj.__mapper__.iterate_properties:
            if not isinstance(prop. sa.orm.ColumnProperty):
                continue
            if isinstance(prop.columns[0].type, UUIDType):
                setattr(obj, prop.key, uuid())

@mehcode
Copy link
Collaborator Author

mehcode commented Jul 9, 2013

That looks sufficient. The point is for primary_keys that are UUIDs. I don't want to have to import uuid and specify default=uuid.uuid4 on each one.

@kvesteri
Copy link
Owner

kvesteri commented Jul 9, 2013

The more that I think about this we could actually make this part of SQLAlchemy-Defaults (https://github.com/kvesteri/sqlalchemy-defaults). Let's make a default rule there: all columns which are primary keys and UUIDType have column default uuid.uuid4

@mehcode
Copy link
Collaborator Author

mehcode commented Jul 15, 2013

Moving to #26

@mehcode mehcode closed this as completed Jul 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants