-
Notifications
You must be signed in to change notification settings - Fork 317
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
Comments
Good job with this one too! Some suggestions:
import uuid
import sqlalchemy as sa
from sqlalchemy_utils.types import UUIDType
class ModelWithType(Base):
id = sa.Column(
UUIDType(version=4),
primary_key=True,
)
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. |
My only reservation with making this a type is I can't quite wrap my head around how to make |
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()) |
That looks sufficient. The point is for primary_keys that are UUIDs. I don't want to have to import |
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 |
Moving to #26 |
I'm thinking of the following primary features:
uuid.UUID
to aUUID
type in postgres, mysql / mariadb (new versions), etc. and to aBINARY(16)
in other dialects. This could also use the coercion listener to auto-convert strings, etc. touuid.UUID
.sa.Column
and adds various arguments that map to arguments foruuid.UUID
and anauto=True|False
flag that will make using as a primary_key easier.The text was updated successfully, but these errors were encountered: