Skip to main content

Utility collection for development with SQLalchemy ORM.

Project description

Read The Docs build Release PyPI downloads count MIT License Python version support GitHub continuous integration codecov.io status for master branch

Utility collection for development with SQLAlchemy 1.4 / 2.0 ORM.

Documentation

https://sqlalchemy-things.readthedocs.io

Installation

Installing sqlalchemy-things with pip:

pip install sqlalchemy-things

Examples

Single table inheritance

from sqlalchemy_things.declarative import (
    IntegerPrimaryKeyMixin,
    PolymorphicMixin,
)

metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)


class Parent(Base, IntegerPrimaryKeyMixin, PolymorphicMixin):
    __tablename__ = 'single_table'


class ChildA(Parent):
    __mapper_args__ = {'polymorphic_identity': 'child_a'}
    some_field = sa.Column(sa.String(255))


class ChildB(Parent):
    __mapper_args__ = {'polymorphic_identity': 'child_b'}
    other_filed = sa.Column(sa.String(127))

Joined table inheritance with cascade primary key mixins

from sqlalchemy_things.declarative import (
    CascadeIntegerPrimaryKeyMixin,
    PolymorphicMixin,
)

metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)


class Parent(Base, CascadeIntegerPrimaryKeyMixin, PolymorphicMixin):
    __tablename__ = 'cascade_pk_parent_table'


class ChildA(Parent):
    __tablename__ = 'cascade_pk_child_table_a'
    __mapper_args__ = {'polymorphic_identity': 'child_a'}
    some_field = sa.Column(sa.String(255))


class ChildB(Parent):
    __tablename__ = 'cascade_pk_child_table_b'
    __mapper_args__ = {'polymorphic_identity': 'child_b'}
    some_field = sa.Column(sa.String(127))

Joined table inheritance with simple primary key mixins

from sqlalchemy_things.declarative import (
    IntegerPrimaryKeyMixin,
    ParentPrimaryKeyMixin,
    PolymorphicMixin,
)

metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)


class Parent(Base, IntegerPrimaryKeyMixin, PolymorphicMixin):
    __tablename__ = 'inherited_pk_parent_table'


class ChildA(ParentPrimaryKeyMixin, Parent):
    __tablename__ = 'inherited_pk_child_table_a'
    __mapper_args__ = {'polymorphic_identity': 'child_a'}
    some_field = sa.Column(sa.String(255))


class ChildB(ParentPrimaryKeyMixin, Parent):
    __tablename__ = 'inherited_pk_child_table_b'
    __mapper_args__ = {'polymorphic_identity': 'child_b'}
    some_field = sa.Column(sa.String(127))

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-things-0.6.1.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

sqlalchemy_things-0.6.1-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

Supported by

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