Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_bundle_model-0.2.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_bundle_model-0.2.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_bundle_model-0.2.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_bundle_model-0.2.1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/20.3.0

File hashes

Hashes for sqlalchemy_bundle_model-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b7388ac8939dfc0850d1aa886894accd283bec6028e60a08e753122733688158
MD5 8188e6756b24d2b0f3a9b1e05156a4b1
BLAKE2b-256 95dc99b39d5ef9bf4bb36f2a7abfcde722133ec32f0b89d12a577983392f8280

See more details on using hashes here.

File details

Details for the file sqlalchemy_bundle_model-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_bundle_model-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bac69090456cb00c20342aebda71a6910320bc06c5108da520c582fc57ba8a91
MD5 2ff6d5d7c12bb88d3643ebf73b5135d5
BLAKE2b-256 778ab42f2b8315c68d87ad0236eb494133d2a43ebfd080e82d050dcd812be4cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page