ormar
Overview
The ormar package is an async mini ORM for Python, with support for Postgres,
MySQL, and SQLite.
The main benefit of using ormar are:
- getting an async ORM that can be used with async frameworks (fastapi, starlette etc.)
- getting just one model to maintain - you don't have to maintain pydantic and other orm model (sqlalchemy, peewee, gino etc.)
The goal was to create a simple ORM that can be used directly (as request and response models) with fastapi that bases it's data validation on pydantic.
Ormar - apart form obvious ORM in name - get it's name from ormar in swedish which means snakes, and ormar(e) in italian which means cabinet.
And what's a better name for python ORM than snakes cabinet :)
Documentation
Check out the documentation for details.
Dependencies
Ormar is built with:
SQLAlchemy corefor query building.databasesfor cross-database async support.pydanticfor data validation.
Migrations
Because ormar is built on SQLAlchemy core, you can use alembic to provide
database migrations.
ormar is still under development: We recommend pinning any dependencies with ormar~=0.3.6
Quick Start
Note: Use ipython to try this from the console, since it supports await.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
Data types
Relation types
- One to many - with
ForeignKey - Many to many - with
Many2Many
Model fields types
Available Model Fields (with required args - optional ones in docs):
String(max_length)Text()Boolean()Integer()Float()Date()Time()DateTime()JSON()BigInteger()Decimal(scale, precision)UUID()ForeignKey(to)Many2Many(to, through)
Available fields options
The following keyword arguments are supported on all field types.
primary_key: boolnullable: booldefault: Anyserver_default: Anyindex: boolunique: boolchoices: typing.Sequence
All fields are required unless one of the following is set:
nullable- Creates a nullable column. Sets the default toNone.default- Set a default value for the field.server_default- Set a default value for the field on server side (like sqlalchemy'sfunc.now()).primary keywithautoincrement- When a column is set to primary key and autoincrement is set on this column. Autoincrement is set by default on int primary keys.