easy create nested models for sqlalchemy
Project description
sqlalchemy-nest
sqlalchemy-nest is easy create nested models for sqlalchemy
Installation
pip install sqlalchemy-nest
Create Nested Model
-
Set declarative_base constructor
use
declarative_nested_model_constructor
for declarative_base constructorfrom sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy.orm import declarative_base, relationship from sqlalchemy_nest import declarative_nested_model_constructor Base = declarative_base(constructor=declarative_nested_model_constructor) class Root(Base): __tablename__ = "root" id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(100)) branches = relationship("Branch", back_populates="root", uselist=True, lazy="joined") class Branch(Base): __tablename__ = "branch" id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(100)) root_id = Column(Integer, ForeignKey("root.id")) root = relationship("Root")
-
Initialization from **kwargs
sets attributes on the constructed instance using the names and values in kwargs.
root = { 'name': 'root', 'branches': [ { 'name': 'branch', }, ] } >>> session.add(Root(**root)) >>> session.commit() >>> added_root: Root = session.query(Root).filter(Root.id == 1).first() Root(id=1, name='root', branches=[Branch(id=1, name='branch', root_id=1)])
Merge Nested Model
-
Set declarative_base constructor and cls
use
declarative_nested_model_constructor
andBaseModel
for declarative_base⚠ sqlalchemy-nest checks viewonly to decide whether to update or not, so please set the viewonly property in the relationship. ⚠
from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy.orm import declarative_base, relationship from sqlalchemy_nest import declarative_nested_model_constructor from sqlalchemy_nest.orm import BaseModel Base = declarative_base(cls=BaseModel, constructor=declarative_nested_model_constructor) class Root(Base): __tablename__ = "root" id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(100)) branches = relationship("Branch", back_populates="root", uselist=True, lazy="joined") class Branch(Base): __tablename__ = "branch" id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(100)) root_id = Column(Integer, ForeignKey("root.id")) root = relationship("Root", viewonly=True)
-
Update from **kwargs
root = { 'name': 'root', 'branches': [ { 'name': 'branch', }, ] } >>> session.add(Root(**root)) >>> session.commit() >>> added_root: Root = session.query(Root).filter(Root.id == 1).first() Root(id=1, name='root', branches=[Branch(id=1, name='branch', root_id=1)]) update_root = { 'id': 1, 'name': 'updated_root', 'branches': [ { 'id': 1, 'name': 'updated_branch', }, { 'name': 'created_branch', }, ] } >>> added_root.merge(**update_root) >>> session.commit() >>> updated_root: Root = session.query(Root).filter(Root.id == 1).first() Root(id=1, name='updated_root', branches=[Branch(id=1, name='updated_branch', root_id=1), Branch(id=2, name='created_branch', root_id=1)])
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-nest-1.1.0.tar.gz
(6.6 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemy_nest-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c1ac1724e17d430b6ce8be47e1aaed4406ec4e9c4bdea1035982d6c7401cc8a |
|
MD5 | 190fffd0afb6bfcdf431e2bb2a25ab17 |
|
BLAKE2b-256 | 35f03305ec3e6aebdcd1b720e6477e3b3c528e48abaaaf9865e14d9542d881eb |