Skip to main content

A simple auto-translator from sqlalchemy ORM models to ormar models.

Project description

Pypi version Pypi version Build Status

sqlalchemy-to-ormar

A simple auto-translator from sqlalchemy ORM models to ormar models.

The ormar package is an async mini ORM for Python, with support for Postgres, MySQL, and SQLite.

To learn more about ormar:

Quickstart

from databases import Database
from sqlalchemy import (
    Column,
    ForeignKey,
    Integer,
    MetaData,
    String,
    create_engine,
    DECIMAL,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship, sessionmaker

Base = declarative_base()
Database_URL = "sqlite:///test.db"
engine = create_engine(Database_URL, echo=True)


# given sqlalchemy models you already have
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    nickname = Column(String)
    salary = Column(DECIMAL)

    addresses = relationship(
        "Address", back_populates="user", cascade="all, delete, delete-orphan"
    )


class Address(Base):
    __tablename__ = "addresses"
    id = Column(Integer, primary_key=True)
    email_address = Column(String, nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"))

    user = relationship("User", back_populates="addresses")


# instantiate new Databases instance
database = Database(Database_URL)
# note that you need new metadata instance as table names in ormar
# will remain the same and you cannot have two tables with same name in
# one metadata, note that we bind it to the same engine! 
# (or you can create new one with same url) 
metadata = MetaData(engine)

# use sqlalchemy-to-ormar (not normally imports should be at the top)
from sqlalchemy_to_ormar import ormar_model_str_repr, sqlalchemy_to_ormar

# convert sqlalchemy models to ormar
OrmarAddress = sqlalchemy_to_ormar(Address, database=database, metadata=metadata)
OrmarUser = sqlalchemy_to_ormar(User, database=database, metadata=metadata)

# you can print the ormar model
# or save it to file and you have proper model definition created for you

address_str = ormar_model_str_repr(OrmarAddress)

# now you can print it or save to file
print(address_str)
# will print:

# class OrmarAddress(ormar.Model):
# 
#     class Meta(ormar.ModelMeta):
#         metadata=metadata
#         database=database
#         tablename="addresses"
# 
#     id = ormar.Integer(autoincrement=True, primary_key=True)
#     email_address = ormar.String(max_length=255, nullable=False)
#     user = ormar.ForeignKey(to=OrmarUser, related_name="addresses", name=user_id, nullable=True)

# if you want to skip column aliases if they match field names use skip_names_if_match flag
user_model_str = ormar_model_str_repr(OrmarUser, skip_names_if_match=True)

# let's insert some sample data with sync sqlalchemy

Base.metadata.create_all(engine)
LocalSession = sessionmaker(bind=engine)
db: Session = LocalSession()

ed_user = User(name="ed", fullname="Ed Jones", nickname="edsnickname")
address = Address(email_address="ed@example.com")
address2 = Address(email_address="eddy@example.com")
ed_user.addresses = [address, address2]

db.add(ed_user)
db.commit()

# and now we can query it asynchronously with ormar
async def test_ormar_queries(): 
    user = await OrmarUser.objects.select_related("addresses").get(name='ed')
    assert len(user.addresses) == 2
    assert user.nickname == 'edsnickname'
    assert user.fullname == 'Ed Jones'

    addresses = await OrmarAddress.objects.select_related('user').all(user__name='ed')
    assert len(addresses) == 2
    assert addresses[0].user.nickname == 'edsnickname'
    assert addresses[1].user.nickname == 'edsnickname'

# run async
import asyncio
asyncio.run(test_ormar_queries())

# drop db
Base.metadata.drop_all(engine)

Automap support

You can use sqlacodegen to generate sqlalchemy models out of existing database and then use sqlalchemy-to-ormar to translate it to ormar models.

Note that sqlalchemy has it's own automap feature, but out of experience it does not work well with complicated databases.

Supported fields

sqlalchemy-to-ormar supports following sqlalchemy field types:

  • "integer": ormar.Integer,
  • "small_integer": ormar.Integer,
  • "big_integer": ormar.BigInteger,
  • "string": ormar.String,
  • "text": ormar.Text,
  • "float": ormar.Float,
  • "decimal": ormar.Decimal,
  • "date": ormar.Date,
  • "datetime": ormar.DateTime,
  • "time": ormar.Time,
  • "boolean": ormar.Boolean

Supported relations

sqlalchemy-to-ormar supports both ForeignKey as well as ManyToMany relations although like ormar itself it will create relation field on one side of the relation only and other side will be auto-populated with reversed side.

Known limitations

sqlalchemy to ormar right now does not support:

  • composite (multi-column) primary keys and foreign keys (as ormar does not support them yet)
  • cascade options from relationship are ignored, only the ones declared in sqlalchemy ForeignKey (ondelete, onupdate) are extracted
  • ManyToMany fields names customization (as ormar does not support them yet)
  • ManyToMany association table has to have primary key
  • Model inheritance

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-to-ormar-0.0.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_to_ormar-0.0.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-to-ormar-0.0.2.tar.gz.

File metadata

  • Download URL: sqlalchemy-to-ormar-0.0.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for sqlalchemy-to-ormar-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2e874059189de8460493fb8038898339d8fa2ba031406768138934acf6221e36
MD5 399c0da470d3223bc22364e90df2b865
BLAKE2b-256 abf673f7de120eae78df1be5972865deba84c270c49d3fbb55d83812af5c3298

See more details on using hashes here.

File details

Details for the file sqlalchemy_to_ormar-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_to_ormar-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for sqlalchemy_to_ormar-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a4e7bea3aaa27cb6f9794562b288e1f70ab6ec980ce6c41865f555220de3371
MD5 a7cd039865a577f86cedc17116bb1b7e
BLAKE2b-256 dbf9ae7c22562d7ae0a4c90acd44d3e4d3e1ab684a479e027be2ecbf16ba0c74

See more details on using hashes here.

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