Skip to main content

SQLAlchemy-OrderingItem provides an OrderingItem base for children of orderinglist relationships.

Project description

SQLAlchemy-OrderingItem provides an OrderingItem base for children of orderinglist relationships. Children of orderinglist relationships will exhibit more intuitive behavior when setting their parent attribute.

Installation

$ pip install sqlalchemy-orderingitem

Quickstart

from sqlalchemy_orderingitem import OrderingItem

from sqlalchemy import Column, ForeignKey, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.orm import sessionmaker, relationship

# standard session creation
engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()


class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)

    # declare orderinglist relationships
    children = relationship(
        'Child', 
        backref='parent',
        order_by='Child.index',
        collection_class=ordering_list('index')
    )


# subclass OrderingItem for children of orderinglist relationships
class Child(OrderingItem, Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    index = Column(Integer)


# create database tables and instantiate parent and child
Base.metadata.create_all(engine)
parent = Parent()
child = Child()
session.add_all([parent, child])
session.commit()

# example 1: setting a child's parent attribute to Parent object
# we expect the child's index to be 0
child.parent = parent
print(child.index)

# example 2: setting a child's parent attribute to None
# we expect the child's index to be None
parent.children = [c]
child.parent = None
print(child.index)

Out:

0
None

If Child did not subclass OrderingItem, we would observe the following unintuitive output:

None
0

Citation

@software{bowen2020sqlalchemy-orderingitem,
  author = {Dillon Bowen},
  title = {SQLAlchemy-OrderingItem},
  url = {https://dsbowen.github.io/sqlalchemy-orderingitem/},
  date = {2020-06-05},
}

License

Users must cite this package in any publications which use it.

It is licensed with the MIT License.

Project details


Download files

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

Files for sqlalchemy-orderingitem, version 0.0.5
Filename, size File type Python version Upload date Hashes
Filename, size sqlalchemy_orderingitem-0.0.5-py3-none-any.whl (4.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size sqlalchemy-orderingitem-0.0.5.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page