{"id":16543056,"url":"https://github.com/collerek/sqlalchemy-to-ormar","last_synced_at":"2025-10-28T15:31:07.774Z","repository":{"id":57470501,"uuid":"355256537","full_name":"collerek/sqlalchemy-to-ormar","owner":"collerek","description":"Simple translator from sqlalchemy ORM models to ormar models","archived":false,"fork":false,"pushed_at":"2021-05-16T14:12:22.000Z","size":36,"stargazers_count":12,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-07T04:10:41.910Z","etag":null,"topics":["asyncio","orm-models","ormar","sqlalchemy","translator"],"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/collerek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"collerek"}},"created_at":"2021-04-06T16:24:03.000Z","updated_at":"2023-07-13T02:25:24.000Z","dependencies_parsed_at":"2022-09-20T12:43:29.354Z","dependency_job_id":null,"html_url":"https://github.com/collerek/sqlalchemy-to-ormar","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/collerek%2Fsqlalchemy-to-ormar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/collerek%2Fsqlalchemy-to-ormar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/collerek%2Fsqlalchemy-to-ormar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/collerek%2Fsqlalchemy-to-ormar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/collerek","download_url":"https://codeload.github.com/collerek/sqlalchemy-to-ormar/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219869256,"owners_count":16555573,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["asyncio","orm-models","ormar","sqlalchemy","translator"],"created_at":"2024-10-11T18:59:13.616Z","updated_at":"2025-10-28T15:31:02.485Z","avatar_url":"https://github.com/collerek.png","language":"Python","funding_links":["https://github.com/sponsors/collerek"],"categories":[],"sub_categories":[],"readme":"\u003cp\u003e\n\u003ca href=\"https://pypi.org/project/sqlalchemy-to-ormar\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/v/sqlalchemy-to-ormar.svg\" alt=\"Pypi version\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/sqlalchemy-to-ormar\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/pyversions/sqlalchemy-to-ormar.svg\" alt=\"Pypi version\"\u003e\n\u003c/a\u003e\n\u003cimg src=\"https://github.com/collerek/sqlalchemy-to-ormar/workflows/build/badge.svg\" alt=\"Build Status\"\u003e\n\u003ca href=\"https://codeclimate.com/github/collerek/sqlalchemy-to-ormar/maintainability\"\u003e\u003cimg src=\"https://api.codeclimate.com/v1/badges/e3ce9277f8373d22afb9/maintainability\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/collerek/sqlalchemy-to-ormar\"\u003e\n  \u003cimg src=\"https://codecov.io/gh/collerek/sqlalchemy-to-ormar/branch/main/graph/badge.svg?token=1FPH7A4Z8P\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://codeclimate.com/github/collerek/sqlalchemy-to-ormar/test_coverage\"\u003e\u003cimg src=\"https://api.codeclimate.com/v1/badges/e3ce9277f8373d22afb9/test_coverage\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://pepy.tech/project/sqlalchemy-to-ormar\"\u003e\n\u003cimg src=\"https://pepy.tech/badge/sqlalchemy-to-ormar\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# sqlalchemy-to-ormar\n\nA simple auto-translator from `sqlalchemy` ORM models to `ormar` models.\n\nThe `ormar` package is an async mini ORM for Python, with support for **Postgres,\nMySQL**, and **SQLite**.\n\nTo learn more about ormar:\n\n* ormar [github][github]\n* ormar [documentation][documentation]\n\n## Quickstart\n\n```python\nfrom databases import Database\nfrom sqlalchemy import (\n    Column,\n    ForeignKey,\n    Integer,\n    MetaData,\n    String,\n    create_engine,\n    DECIMAL,\n)\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy.orm import Session, relationship, sessionmaker\n\nBase = declarative_base()\nDatabase_URL = \"sqlite:///test.db\"\nengine = create_engine(Database_URL, echo=True)\n\n\n# given sqlalchemy models you already have\nclass User(Base):\n    __tablename__ = \"users\"\n\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n    fullname = Column(String)\n    nickname = Column(String)\n    salary = Column(DECIMAL)\n\n    addresses = relationship(\n        \"Address\", back_populates=\"user\", cascade=\"all, delete, delete-orphan\"\n    )\n\n\nclass Address(Base):\n    __tablename__ = \"addresses\"\n    id = Column(Integer, primary_key=True)\n    email_address = Column(String, nullable=False)\n    user_id = Column(Integer, ForeignKey(\"users.id\"))\n\n    user = relationship(\"User\", back_populates=\"addresses\")\n\n\n# instantiate new Databases instance\ndatabase = Database(Database_URL)\n# note that you need new metadata instance as table names in ormar\n# will remain the same and you cannot have two tables with same name in\n# one metadata, note that we bind it to the same engine! \n# (or you can create new one with same url) \nmetadata = MetaData(engine)\n\n# use sqlalchemy-to-ormar (not normally imports should be at the top)\nfrom sqlalchemy_to_ormar import ormar_model_str_repr, sqlalchemy_to_ormar\n\n# convert sqlalchemy models to ormar\nOrmarAddress = sqlalchemy_to_ormar(Address, database=database, metadata=metadata)\nOrmarUser = sqlalchemy_to_ormar(User, database=database, metadata=metadata)\n\n# you can print the ormar model\n# or save it to file and you have proper model definition created for you\n\naddress_str = ormar_model_str_repr(OrmarAddress)\n\n# now you can print it or save to file\nprint(address_str)\n# will print:\n\n# class OrmarAddress(ormar.Model):\n# \n#     class Meta(ormar.ModelMeta):\n#         metadata=metadata\n#         database=database\n#         tablename=\"addresses\"\n# \n#     id = ormar.Integer(autoincrement=True, primary_key=True)\n#     email_address = ormar.String(max_length=255, nullable=False)\n#     user = ormar.ForeignKey(to=OrmarUser, related_name=\"addresses\", name=user_id, nullable=True)\n\n# if you want to skip column aliases if they match field names use skip_names_if_match flag\nuser_model_str = ormar_model_str_repr(OrmarUser, skip_names_if_match=True)\n\n# let's insert some sample data with sync sqlalchemy\n\nBase.metadata.create_all(engine)\nLocalSession = sessionmaker(bind=engine)\ndb: Session = LocalSession()\n\ned_user = User(name=\"ed\", fullname=\"Ed Jones\", nickname=\"edsnickname\")\naddress = Address(email_address=\"ed@example.com\")\naddress2 = Address(email_address=\"eddy@example.com\")\ned_user.addresses = [address, address2]\n\ndb.add(ed_user)\ndb.commit()\n\n# and now we can query it asynchronously with ormar\nasync def test_ormar_queries(): \n    user = await OrmarUser.objects.select_related(\"addresses\").get(name='ed')\n    assert len(user.addresses) == 2\n    assert user.nickname == 'edsnickname'\n    assert user.fullname == 'Ed Jones'\n    \n    addresses = await OrmarAddress.objects.select_related('user').all(user__name='ed')\n    assert len(addresses) == 2\n    assert addresses[0].user.nickname == 'edsnickname'\n    assert addresses[1].user.nickname == 'edsnickname'\n\n# run async\nimport asyncio\nasyncio.run(test_ormar_queries())\n\n# drop db\nBase.metadata.drop_all(engine)\n```\n\n## Automap support\n\nYou can use [`sqlacodegen`](https://github.com/agronholm/sqlacodegen) to generate sqlalchemy models out of existing database \nand then use sqlalchemy-to-ormar to translate it to `ormar` models. \n\nNote that sqlalchemy has it's own automap feature, but out of experience it does not work well with complicated databases.\n\n## Supported fields\n\n`sqlalchemy-to-ormar` supports following sqlalchemy field types:\n\n* \"integer\": `ormar.Integer`,\n* \"small_integer\": `ormar.Integer`,\n* \"big_integer\": `ormar.BigInteger`,\n* \"string\": `ormar.String,`\n* \"text\": `ormar.Text,`\n* \"float\": `ormar.Float,`\n* \"decimal\": `ormar.Decimal,`\n* \"date\": `ormar.Date,`\n* \"datetime\": `ormar.DateTime,`\n* \"time\": `ormar.Time,`\n* \"boolean\": `ormar.Boolean`\n\n## Supported relations\n\nsqlalchemy-to-ormar supports both `ForeignKey` as well as `ManyToMany` relations\nalthough like `ormar` itself it will create relation field on one side of the relation only\nand other side will be auto-populated with reversed side.\n\n## Known limitations\n\nsqlalchemy to ormar right now does not support:\n\n* composite (multi-column) primary keys and foreign keys (as ormar does not support them\n  yet)\n* `cascade` options from `relationship` are ignored, only the ones declared in sqlalchemy ForeignKey (ondelete, onupdate) are extracted\n* ManyToMany fields names customization (as ormar does not support them yet)\n* ManyToMany association table has to have primary key\n* Model inheritance\n\n[documentation]: https://collerek.github.io/ormar/\n[github]: https://github.com/collerek/ormar","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcollerek%2Fsqlalchemy-to-ormar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcollerek%2Fsqlalchemy-to-ormar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcollerek%2Fsqlalchemy-to-ormar/lists"}