Comments (6)
Good job with this one too!
Some suggestions:
- 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,
)
- Some users may want to store the UUID as VARCHAR(32), this should also be configuration option.
- 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.
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.
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.
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.
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.
Moving to #26
from sqlalchemy-utils.
Related Issues (20)
- Unable to call refresh_materialized_view on CRDB w/SQLAlchemy 2.0 HOT 1
- Create SECURITY.md
- CompositeType support for pg8000 and psycopg3 HOT 1
- create_view not accepting `replace` argument HOT 2
- Helper functions for explicit table locks ?
- custom aggregated column does not work with nullable=False
- Database_exist not working in python 3.11 HOT 1
- Even though binary was set false, impl remain binary(16)
- generic_relationship breaks with SqlAlchemy 2.0.22 HOT 3
- [Feature] Add MySQL collation support in `create_database`
- DeprecationWarning: 'crypt' is deprecated HOT 1
- Issue with change of implementation of EncryptedType from ‘LargeBinary’ to ‘String’ HOT 1
- [Feature] Enable Cascade Behavior for generic_relationship
- [Question] how to encrypt using aws kms in sqlalchemy
- Error on model with `server_default` for `DateTime` column and `UUIDType` primary key.
- [Feature] database helpers functions don't support custom create_engine()
- New release for SQLAlchemy >= 2.0.22 HOT 19
- Issue updating aggregate when setting foreign key to Null HOT 1
- Password checking error when using SQLAlchemy and Postgres
- [Feature] `auto_delete_orphans` not support `back_populates` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlalchemy-utils.