An extension to SQLAlchemy to treat aggregated columns and clauses as Models
Project description
sqlalchemy_bundle_model
An extension to SQLAlchemy to treat aggregated columns and clauses as Models
installation
$ pip install sqlalchemy-bundle-model
usage
>>> from sqlalchemy import Column, BigInteger, Text, ForeignKey
>>> from sqlalchemy.orm import declarative_base, relationship, sessionmaker
>>> from sqlalchemy.engine import create_engine
>>> from sqlalchemy_bundle_model import BundleModel, col
>>> DeclarativeBase = declarative_base()
>>> class User(DeclarativeBase):
... __tablename__ = "users"
... id = Column(BigInteger, primary_key=True)
... name = Column(Text, nullable=False)
... group_id = Column(ForeignKey("groups.id"), nullable=False)
...
... group = relationship("Group")
...
>>> class Group(DeclarativeBase):
... __tablename__ = "groups"
... id = Column(BigInteger, primary_key=True)
... name = Column(Text, nullable=False)
...
>>> class GroupUser(BundleModel):
... id = col(int, User.id)
... name = col(str, User.name)
... group_name = col(str, Group.name)
...
... @staticmethod
... def join(_query):
... return _query.join(User.group)
...
>>> engine = create_engine("sqlite://")
>>> DeclarativeBase.metadata.create_all(bind=engine)
>>> session_cls = sessionmaker(bind=engine)
>>> session = session_cls()
>>> user = User(id=1, name="John Doe")
>>> group = Group(id=1, name="A")
>>> user.group = group
>>> session.add(user)
>>> session.commit()
>>> query = session.query(GroupUser)
>>> query = GroupUser.join(query)
>>> result = query.first()
>>> result.group_name == "A"
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
Built Distribution
Close
Hashes for sqlalchemy_bundle_model-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7388ac8939dfc0850d1aa886894accd283bec6028e60a08e753122733688158 |
|
MD5 | 8188e6756b24d2b0f3a9b1e05156a4b1 |
|
BLAKE2b-256 | 95dc99b39d5ef9bf4bb36f2a7abfcde722133ec32f0b89d12a577983392f8280 |
Close
Hashes for sqlalchemy_bundle_model-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bac69090456cb00c20342aebda71a6910320bc06c5108da520c582fc57ba8a91 |
|
MD5 | 2ff6d5d7c12bb88d3643ebf73b5135d5 |
|
BLAKE2b-256 | 778ab42f2b8315c68d87ad0236eb494133d2a43ebfd080e82d050dcd812be4cb |