Package: ormantic
OrmanticTheormanticpackage is an async ORM for Python, with support for Postgres,
MySQL, and SQLite. Ormatic is a fork fromORMfor the purpose of
integrating withFastAPIand is built with:SQLAlchemy corefor query building.databasesfor cross-database async support.pydanticfor data validation.Because ORM is built on SQLAlchemy core, you can use Alembic to provide
database migrations.Note: Useipythonto try this from the console, since it supportsawait.importdatabasesimportsqlalchemyimportormanticasormdatabase=databases.Database("sqlite:///db.sqlite")metadata=sqlalchemy.MetaData()classNote(orm.Model):id:orm.Integer(primary_key=True)=Nonetext:orm.String(max_length=100)completed:orm.Boolean()=FalseclassMapping:table_name="notes"database=databasemetadata=metadata# Create the databaseengine=sqlalchemy.create_engine(str(database.url))metadata.create_all(engine)# .create()awaitNote.objects.create(text="Buy the groceries.",completed=False)awaitNote.objects.create(text="Call Mum.",completed=True)awaitNote.objects.create(text="Send invoices.",completed=True)# .all()notes=awaitNote.objects.all()# .filter()notes=awaitNote.objects.filter(completed=True).all()# exact, iexact, contains, icontains, lt, lte, gt, gte, innotes=awaitNote.objects.filter(text__icontains="mum").all()# .get()note=awaitNote.objects.get(id=1)# .update()awaitnote.update(completed=True)# .delete()awaitnote.delete()note=awaitNote.objects.get(id=2)assertnote.pk==note.id==2Ormantic supports loading and filtering across foreign keys...importdatabasesimportsqlalchemyimportormanticasormdatabase=databases.Database("sqlite:///db.sqlite")metadata=sqlalchemy.MetaData()classAlbum(orm.Model):id:orm.Integer(primary_key=True)=Nonename:orm.String(max_length=100)classMapping:table_name="album"metadata=metadatadatabase=databaseclassTrack(orm.Model):id:orm.Integer(primary_key=True)=Nonealbum:orm.ForeignKey(Album)title:orm.String(max_length=100)position:orm.Integer()classMapping:table_name="track"metadata=metadatadatabase=database# Create the databaseengine=sqlalchemy.create_engine(str(database.url))metadata.create_all(engine)# Create some records to work with.malibu=awaitAlbum.objects.create(name="Malibu")awaitTrack.objects.create(album=malibu,title="The Bird",position=1)awaitTrack.objects.create(album=malibu,title="Heart don't stand a chance",position=2)awaitTrack.objects.create(album=malibu,title="The Waters",position=3)fantasies=awaitAlbum.objects.create(name="Fantasies")awaitTrack.objects.create(album=fantasies,title="Help I'm Alive",position=1)awaitTrack.objects.create(album=fantasies,title="Sick Muse",position=2)# Fetch an instance, without loading a foreign key relationship on it.track=awaitTrack.objects.get(title="The Bird")# We have an album instance, but it only has the primary key populatedprint(track.album)# Album(id=1) [sparse]print(track.album.pk)# 1print(track.album.name)# Raises AttributeError# Load the relationship from the databaseawaittrack.album.load()asserttrack.album.name=="Malibu"# This time, fetch an instance, loading the foreign key relationship.track=awaitTrack.objects.select_related("album").get(title="The Bird")asserttrack.album.name=="Malibu"# Fetch instances, with a filter across an FK relationship.tracks=Track.objects.filter(album__name="Fantasies")assertlen(tracks)==2# Fetch instances, with a filter and operator across an FK relationship.assertTrack.objects.filter(album__name__iexact="fantasies").count()==2Data typesThe following keyword arguments are supported on all field types.primary_keyallow_nullindexuniqueThe following column types are supported:orm.Boolean()orm.Date()orm.DateTime()orm.Enum()orm.Float()orm.Integer()orm.JSON()orm.String(max_length)orm.StringArray()orm.Text()orm.Time()