No project description provided
Project description
sqlalchemy GUID
This is a simple extension to SQLAlchemy that adds support for UUID with postgresql compatibility to other database dialects.
Installation
pip install sqlalchemy_guid
Usage
Basically, you just need to import the GUID type and use it in your models:
import uuid
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer
from sqlalchemy_guid import GUID
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
guid = Column(GUID, default=uuid.uuid4)
For postgresql it will use the native uuid type, for other database dialects it will use a CHAR(32), storing as stringified hex values.
Now, when you create a new user, the guid will be automatically generated:
user = User()
session.add(user)
session.commit()
print(user.guid)
It will also work with other database dialects, such as sqlite:
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
session = Session(engine)
user = User()
session.add(user)
session.commit()
print(user.guid)
More sqlite examples can be found in the tests.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sqlalchemy_guid-1.0.1.tar.gz
(2.6 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemy_guid-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94f2ba6d886b6a28f9ece68e7e5d5fe7bbda54aa47680e45f859ee15b10540ad |
|
MD5 | a29b67d2dc56320063e82eb67b473b96 |
|
BLAKE2b-256 | a24a7bed381afc53a411cfc6b16752010a714db4d9ca0c2a882ba41d953c9f6b |