Giter Club home page Giter Club logo

Comments (6)

kvesteri avatar kvesteri commented on July 29, 2024 1

Good job with this one too!

Some suggestions:

  1. As with #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.

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

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?

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 29, 2024

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())

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

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.

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 29, 2024

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

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

Moving to #26

from sqlalchemy-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.