{"id":30538368,"url":"https://github.com/satorudev976/sqlalchemy-nest","last_synced_at":"2026-05-06T00:06:25.295Z","repository":{"id":214960646,"uuid":"737532864","full_name":"satorudev976/sqlalchemy-nest","owner":"satorudev976","description":"Nested Model With SQLAlchemy","archived":false,"fork":false,"pushed_at":"2025-11-27T14:30:58.000Z","size":136,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-27T22:19:33.019Z","etag":null,"topics":["database","nested","orm","python","sqlalchemy"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/satorudev976.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-12-31T12:10:52.000Z","updated_at":"2025-11-27T14:31:02.000Z","dependencies_parsed_at":"2024-01-01T14:47:15.583Z","dependency_job_id":"1331a31b-115b-4594-969d-506bf6c49d6f","html_url":"https://github.com/satorudev976/sqlalchemy-nest","commit_stats":null,"previous_names":["satorudev976/sqlalchemy-nest"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/satorudev976/sqlalchemy-nest","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satorudev976%2Fsqlalchemy-nest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satorudev976%2Fsqlalchemy-nest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satorudev976%2Fsqlalchemy-nest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satorudev976%2Fsqlalchemy-nest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/satorudev976","download_url":"https://codeload.github.com/satorudev976/sqlalchemy-nest/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satorudev976%2Fsqlalchemy-nest/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32672688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-05T11:29:49.557Z","status":"ssl_error","status_checked_at":"2026-05-05T11:29:48.587Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","nested","orm","python","sqlalchemy"],"created_at":"2025-08-27T20:03:07.742Z","updated_at":"2026-05-06T00:06:25.280Z","avatar_url":"https://github.com/satorudev976.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlalchemy-nest\n\n[![PyPI - Version](https://img.shields.io/pypi/v/sqlalchemy-nest)](https://pypi.org/project/sqlalchemy-nest/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqlalchemy-nest)](https://pypi.org/project/sqlalchemy-nest/)\n[![Downloads](https://static.pepy.tech/badge/sqlalchemy-nest)](https://pepy.tech/project/sqlalchemy-nest)\n[![CI](https://github.com/satorudev976/sqlalchemy-nest/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/satorudev976/sqlalchemy-nest/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/satorudev976/sqlalchemy-nest/graph/badge.svg?token=67ESOOAA5E)](https://codecov.io/gh/satorudev976/sqlalchemy-nest)\n[![Maintainability](https://api.codeclimate.com/v1/badges/7c8a77a2447deec781ce/maintainability)](https://codeclimate.com/github/satorudev976/sqlalchemy-nest/maintainability)\n\nsqlalchemy-nest is easy create nested models for sqlalchemy\n\n### Why?? 🧐🧐\n\nThe default constructor of ```declarative_base()``` in sqlalchemy is as follows\n\n```python\ndef _declarative_constructor(self: Any, **kwargs: Any) -\u003e None:\n    cls_ = type(self)\n    for k in kwargs:\n        if not hasattr(cls_, k):\n            raise TypeError(\n                \"%r is an invalid keyword argument for %s\" % (k, cls_.__name__)\n            )\n        setattr(self, k, kwargs[k])\n\n```\n\nSo can’t create nested model by unpacking schema like below. OMG !!!\n\n```python\nfrom sqlalchemy import Column, ForeignKey, Integer, String\nfrom sqlalchemy.orm import declarative_base, relationship\n\nBase = declarative_base()\n\nclass Root(Base):\n    __tablename__ = \"root\"\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    name = Column(String(100))\n\n    branches = relationship(\"Branch\", back_populates=\"root\", uselist=True, lazy=\"joined\")\n\nclass Branch(Base):\n    __tablename__ = \"branch\"\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    name = Column(String(100))\n    root_id = Column(Integer, ForeignKey(\"root.id\"))\n\n    root = relationship(\"Root\")\n\nroot = {\n    'name': 'root',\n    'branches': [\n        {\n            'name': 'branch',\n        },\n    ]\n}\n\ncreated_root = Root(**root)\n\u003e\u003e\u003e AttributeError: 'dict' object has no attribute '_sa_instance_state'\n```\n\n\n### Installation\n\n```\npip install sqlalchemy-nest\n```\n\n### Create Nested Model\n\n1. Set declarative_base constructor\n\n    use ```declarative_nested_model_constructor``` for declarative_base constructor\n\n    ```python\n    from sqlalchemy import Column, ForeignKey, Integer, String\n    from sqlalchemy.orm import declarative_base, relationship\n    from sqlalchemy_nest import declarative_nested_model_constructor\n\n    Base = declarative_base(constructor=declarative_nested_model_constructor)\n\n    class Root(Base):\n        __tablename__ = \"root\"\n\n        id = Column(Integer, primary_key=True, autoincrement=True)\n        name = Column(String(100))\n\n        branches = relationship(\"Branch\", back_populates=\"root\", uselist=True, lazy=\"joined\")\n\n    class Branch(Base):\n        __tablename__ = \"branch\"\n\n        id = Column(Integer, primary_key=True, autoincrement=True)\n        name = Column(String(100))\n        root_id = Column(Integer, ForeignKey(\"root.id\"))\n\n        root = relationship(\"Root\")\n    ```\n\n1. Initialization from **kwargs\n\n    sets attributes on the constructed instance using the names and values in kwargs.\n\n    ```python\n    root = {\n        'name': 'root',\n        'branches': [\n            {\n                'name': 'branch',\n            },\n        ]\n    }\n    \u003e\u003e\u003e session.add(Root(**root))\n    \u003e\u003e\u003e session.commit()\n    \u003e\u003e\u003e added_root: Root = session.query(Root).filter(Root.id == 1).first()\n    Root(id=1, name='root', branches=[\n        Branch(id=1, name='branch', root_id=1)]\n    )\n    ```\n\n### Merge Nested Model\n\n1. Set declarative_base constructor and cls\n\n    use ```declarative_nested_model_constructor```  and ```BaseModel``` for declarative_base\n\n    ```python\n    from sqlalchemy import Column, ForeignKey, Integer, String\n    from sqlalchemy.orm import declarative_base, relationship\n    from sqlalchemy_nest import declarative_nested_model_constructor\n    from sqlalchemy_nest.orm import BaseModel\n\n    Base = declarative_base(cls=BaseModel, constructor=declarative_nested_model_constructor)\n\n    class Root(Base):\n        __tablename__ = \"root\"\n\n        id = Column(Integer, primary_key=True, autoincrement=True)\n        name = Column(String(100))\n\n        branches = relationship(\"Branch\", back_populates=\"root\", uselist=True, lazy=\"joined\")\n\n    class Branch(Base):\n        __tablename__ = \"branch\"\n\n        id = Column(Integer, primary_key=True, autoincrement=True)\n        name = Column(String(100))\n        root_id = Column(Integer, ForeignKey(\"root.id\"))\n\n        root = relationship(\"Root\")\n\n    ```\n\n1. Update from **kwargs\n\n\n    ```python\n    root = {\n        'name': 'root',\n        'branches': [\n            {\n                'name': 'branch',\n            },\n        ]\n    }\n    \u003e\u003e\u003e session.add(Root(**root))\n    \u003e\u003e\u003e session.commit()\n    \u003e\u003e\u003e added_root: Root = session.query(Root).filter(Root.id == 1).first()\n    Root(id=1, name='root', branches=[\n        Branch(id=1, name='branch', root_id=1)]\n    )\n\n    update_root = {\n        'id': 1,\n        'name': 'updated_root',\n        'branches': [\n            {\n                'id': 1,\n                'name': 'updated_branch',\n            },\n            {\n                'name': 'created_branch',\n            },\n        ]\n    }\n    \u003e\u003e\u003e added_root.merge(**update_root)\n    \u003e\u003e\u003e session.commit()\n    \u003e\u003e\u003e updated_root: Root = session.query(Root).filter(Root.id == 1).first()\n    Root(id=1, name='updated_root', branches=[\n        Branch(id=1, name='updated_branch', root_id=1),\n        Branch(id=2, name='created_branch', root_id=1)]\n    )\n    ```\n\n### Development\n\nPlease refer to the [CONTRIBUTING](https://github.com/satorudev976/sqlalchemy-nest/blob/main/CONTRIBUTING.md)\n\n### Example\n\n[Sample Code](https://github.com/satorudev976/sqlalchemy-nest/tree/main/examples) using FastAPI and SQLAlchemy\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsatorudev976%2Fsqlalchemy-nest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsatorudev976%2Fsqlalchemy-nest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsatorudev976%2Fsqlalchemy-nest/lists"}