version with pydantic inheritance passing all the tests
This commit is contained in:
0
docs/fastapi.md
Normal file
0
docs/fastapi.md
Normal file
206
docs/fields.md
Normal file
206
docs/fields.md
Normal file
@ -0,0 +1,206 @@
|
||||
# Fields
|
||||
|
||||
|
||||
There are 11 basic model field types and a special `ForeignKey` field to establish relationships between models.
|
||||
|
||||
Each of the `Fields` has assigned both `sqlalchemy` column class and python type that is used to create `pydantic` model.
|
||||
|
||||
|
||||
## Common Parameters
|
||||
|
||||
All `Field` types have a set of common parameters.
|
||||
|
||||
### primary_key
|
||||
|
||||
`primary_key`: `bool` = `False` -> by default False.
|
||||
|
||||
Sets the primary key column on a table, foreign keys always refer to the pk of the `Model`.
|
||||
|
||||
Used in sql only.
|
||||
|
||||
### autoincrement
|
||||
|
||||
`autoincrement`: `bool` = `primary_key and type == int` -> defaults to True if column is a primary key and of type Integer, otherwise False.
|
||||
|
||||
Can be only used with int fields.
|
||||
|
||||
If a field has autoincrement it becomes optional.
|
||||
|
||||
Used only in sql.
|
||||
|
||||
### nullable
|
||||
|
||||
`nullable`: `bool` = `not primary_key` -> defaults to False for primary key column, and True for all other.
|
||||
|
||||
Specifies if field is optional or required, used both with sql and pydantic.
|
||||
|
||||
!!!note
|
||||
By default all `ForeignKeys` are also nullable, meaning the related `Model` is not required.
|
||||
|
||||
If you change the `ForeignKey` column to `nullable`, it not only becomes required, it changes also the way in which data is loaded in queries.
|
||||
|
||||
If you select `Model` without explicitly adding related `Model` assigned by not nullable `ForeignKey`, the `Model` is still gona be appended automatically, see example below.
|
||||
|
||||
```Python hl_lines="24 32 33 34 35 37 38 39 40 41"
|
||||
--8<-- "../docs_src/fields/docs003.py"
|
||||
```
|
||||
|
||||
!!!info
|
||||
If you want to know more about how you can preload related models during queries and how the relations work read the [queries][queries] and [relations][relations] sections.
|
||||
|
||||
|
||||
### default
|
||||
|
||||
`default`: `Any` = `None` -> defaults to None.
|
||||
|
||||
A default value used if no other value is passed.
|
||||
|
||||
In sql invoked on an insert, used during pydantic model definition.
|
||||
|
||||
If the field has a default value it becomes optional.
|
||||
|
||||
You can pass a static value or a Callable (function etc.)
|
||||
|
||||
Used both in sql and pydantic.
|
||||
|
||||
### server default
|
||||
|
||||
`server_default`: `Any` = `None` -> defaults to None.
|
||||
|
||||
A default value used if no other value is passed.
|
||||
|
||||
In sql invoked on the server side so you can pass i.e. sql function (like now() wrapped in sqlalchemy text() clause).
|
||||
|
||||
If the field has a server_default value it becomes optional.
|
||||
|
||||
You can pass a static value or a Callable (function etc.)
|
||||
|
||||
Used in sql only.
|
||||
|
||||
### index
|
||||
|
||||
`index`: `bool` = `False` -> by default False,
|
||||
|
||||
Sets the index on a table's column.
|
||||
|
||||
Used in sql only.
|
||||
|
||||
### unique
|
||||
|
||||
`unique`: `bool` = `False`
|
||||
|
||||
Sets the unique constraint on a table's column.
|
||||
|
||||
Used in sql only.
|
||||
|
||||
## Fields Types
|
||||
|
||||
### String
|
||||
|
||||
`String(length)` has a required `length` parameter.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.String`
|
||||
* Type (used for pydantic): `str`
|
||||
|
||||
### Text
|
||||
|
||||
`Text()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.Text`
|
||||
* Type (used for pydantic): `str`
|
||||
|
||||
### Boolean
|
||||
|
||||
`Boolean()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.Boolean`
|
||||
* Type (used for pydantic): `bool`
|
||||
|
||||
### Integer
|
||||
|
||||
`Integer()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.Integer`
|
||||
* Type (used for pydantic): `int`
|
||||
|
||||
### BigInteger
|
||||
|
||||
`BigInteger()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.BigInteger`
|
||||
* Type (used for pydantic): `int`
|
||||
|
||||
### Float
|
||||
|
||||
`Float()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.Float`
|
||||
* Type (used for pydantic): `float`
|
||||
|
||||
### Decimal
|
||||
|
||||
`Decimal(lenght, precision)` has required `length` and `precision` parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.DECIMAL`
|
||||
* Type (used for pydantic): `decimal.Decimal`
|
||||
|
||||
### Date
|
||||
|
||||
`Date()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.Date`
|
||||
* Type (used for pydantic): `datetime.date`
|
||||
|
||||
### Time
|
||||
|
||||
`Time()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.Time`
|
||||
* Type (used for pydantic): `datetime.time`
|
||||
|
||||
### DateTime
|
||||
|
||||
`DateTime()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.DateTime`
|
||||
* Type (used for pydantic): `datetime.datetime`
|
||||
|
||||
### JSON
|
||||
|
||||
`JSON()` has no required parameters.
|
||||
|
||||
* Sqlalchemy column: `sqlalchemy.JSON`
|
||||
* Type (used for pydantic): `pydantic.Json`
|
||||
|
||||
### ForeignKey
|
||||
|
||||
`ForeignKey(to, related_name=None)` has required parameters `to` that takes target `Model` class.
|
||||
|
||||
Sqlalchemy column and Type are automatically taken from target `Model`.
|
||||
|
||||
* Sqlalchemy column: class of a target `Model` primary key column
|
||||
* Type (used for pydantic): type of a target `Model` primary key column
|
||||
|
||||
`ForeignKey` fields are automatically registering reverse side of the relation.
|
||||
|
||||
By default it's child (source) `Model` name + s, like courses in snippet below:
|
||||
|
||||
```Python hl_lines="25 31"
|
||||
--8<-- "../docs_src/fields/docs001.py"
|
||||
```
|
||||
|
||||
But you can overwrite this name by providing `related_name` parameter like below:
|
||||
|
||||
```Python hl_lines="25 30"
|
||||
--8<-- "../docs_src/fields/docs002.py"
|
||||
```
|
||||
|
||||
!!!tip
|
||||
Since related models are coming from Relationship Manager the reverse relation on access returns list of `wekref.proxy` to avoid circular references.
|
||||
|
||||
!!!info
|
||||
All relations are stored in lists, but when you access parent `Model` the ormar is unpacking the value for you.
|
||||
Read more in [relations][relations].
|
||||
|
||||
[relations]: ./relations.md
|
||||
[queries]: ./queries.md
|
||||
213
docs/index.md
Normal file
213
docs/index.md
Normal file
@ -0,0 +1,213 @@
|
||||
# ORMar
|
||||
|
||||
<p>
|
||||
<a href="https://pypi.org/project/ormar">
|
||||
<img src="https://img.shields.io/pypi/v/ormar.svg" alt="Pypi version">
|
||||
</a>
|
||||
<a href="https://pypi.org/project/ormar">
|
||||
<img src="https://img.shields.io/pypi/pyversions/ormar.svg" alt="Pypi version">
|
||||
</a>
|
||||
<a href="https://travis-ci.com/collerek/ormar">
|
||||
<img src="https://travis-ci.com/collerek/ormar.svg?branch=master" alt="Build Status">
|
||||
</a>
|
||||
<a href="https://codecov.io/gh/collerek/ormar">
|
||||
<img src="https://codecov.io/gh/collerek/ormar/branch/master/graph/badge.svg" alt="Coverage">
|
||||
</a>
|
||||
<a href="https://www.codefactor.io/repository/github/collerek/ormar">
|
||||
<img src="https://www.codefactor.io/repository/github/collerek/ormar/badge" alt="CodeFactor" />
|
||||
</a>
|
||||
<a href="https://app.codacy.com/manual/collerek/ormar?utm_source=github.com&utm_medium=referral&utm_content=collerek/oramr&utm_campaign=Badge_Grade_Dashboard">
|
||||
<img src="https://api.codacy.com/project/badge/Grade/62568734f70f49cd8ea7a1a0b2d0c107" alt="Codacy" />
|
||||
</a>
|
||||
</p>
|
||||
|
||||
The `ormar` package is an async ORM for Python, with support for Postgres,
|
||||
MySQL, and SQLite. Ormar is built with:
|
||||
|
||||
* [`SQLAlchemy core`][sqlalchemy-core] for query building.
|
||||
* [`databases`][databases] for cross-database async support.
|
||||
* [`pydantic`][pydantic] for data validation.
|
||||
|
||||
Because ormar is built on SQLAlchemy core, you can use [`alembic`][alembic] to provide
|
||||
database migrations.
|
||||
|
||||
The goal was to create a simple ORM that can be used directly with [`fastapi`][fastapi] that bases it's data validation on pydantic.
|
||||
Initial work was inspired by [`encode/orm`][encode/orm].
|
||||
The encode package was too simple (i.e. no ability to join two times to the same table) and used typesystem for data checks.
|
||||
|
||||
**ormar is still under development:** We recommend pinning any dependencies with `ormar~=0.0.1`
|
||||
|
||||
**Note**: Use `ipython` to try this from the console, since it supports `await`.
|
||||
|
||||
```python
|
||||
import databases
|
||||
import ormar
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Note(ormar.Model):
|
||||
__tablename__ = "notes"
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
# primary keys of type int by dafault are set to autoincrement
|
||||
id = ormar.Integer(primary_key=True)
|
||||
text = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
|
||||
# Create the database
|
||||
engine = sqlalchemy.create_engine(str(database.url))
|
||||
metadata.create_all(engine)
|
||||
|
||||
# .create()
|
||||
await Note.objects.create(text="Buy the groceries.", completed=False)
|
||||
await Note.objects.create(text="Call Mum.", completed=True)
|
||||
await Note.objects.create(text="Send invoices.", completed=True)
|
||||
|
||||
# .all()
|
||||
notes = await Note.objects.all()
|
||||
|
||||
# .filter()
|
||||
notes = await Note.objects.filter(completed=True).all()
|
||||
|
||||
# exact, iexact, contains, icontains, lt, lte, gt, gte, in
|
||||
notes = await Note.objects.filter(text__icontains="mum").all()
|
||||
|
||||
# .get()
|
||||
note = await Note.objects.get(id=1)
|
||||
|
||||
# .update()
|
||||
await note.update(completed=True)
|
||||
|
||||
# .delete()
|
||||
await note.delete()
|
||||
|
||||
# 'pk' always refers to the primary key
|
||||
note = await Note.objects.get(pk=2)
|
||||
note.pk # 2
|
||||
```
|
||||
|
||||
Ormar supports loading and filtering across foreign keys...
|
||||
|
||||
```python
|
||||
import databases
|
||||
import ormar
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Track(ormar.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
album = ormar.ForeignKey(Album)
|
||||
title = ormar.String(length=100)
|
||||
position = ormar.Integer()
|
||||
|
||||
|
||||
# Create some records to work with.
|
||||
malibu = await Album.objects.create(name="Malibu")
|
||||
await Track.objects.create(album=malibu, title="The Bird", position=1)
|
||||
await Track.objects.create(album=malibu, title="Heart don't stand a chance", position=2)
|
||||
await Track.objects.create(album=malibu, title="The Waters", position=3)
|
||||
|
||||
fantasies = await Album.objects.create(name="Fantasies")
|
||||
await Track.objects.create(album=fantasies, title="Help I'm Alive", position=1)
|
||||
await Track.objects.create(album=fantasies, title="Sick Muse", position=2)
|
||||
|
||||
|
||||
# Fetch an instance, without loading a foreign key relationship on it.
|
||||
track = await Track.objects.get(title="The Bird")
|
||||
|
||||
# We have an album instance, but it only has the primary key populated
|
||||
print(track.album) # Album(id=1) [sparse]
|
||||
print(track.album.pk) # 1
|
||||
print(track.album.name) # Raises AttributeError
|
||||
|
||||
# Load the relationship from the database
|
||||
await track.album.load()
|
||||
assert track.album.name == "Malibu"
|
||||
|
||||
# This time, fetch an instance, loading the foreign key relationship.
|
||||
track = await Track.objects.select_related("album").get(title="The Bird")
|
||||
assert track.album.name == "Malibu"
|
||||
|
||||
# By default you also get a second side of the relation
|
||||
# constructed as lowercase source model name +'s' (tracks in this case)
|
||||
# you can also provide custom name with parameter related_name
|
||||
album = await Album.objects.select_related("tracks").all()
|
||||
assert len(album.tracks) == 3
|
||||
|
||||
# Fetch instances, with a filter across an FK relationship.
|
||||
tracks = Track.objects.filter(album__name="Fantasies")
|
||||
assert len(tracks) == 2
|
||||
|
||||
# Fetch instances, with a filter and operator across an FK relationship.
|
||||
tracks = Track.objects.filter(album__name__iexact="fantasies")
|
||||
assert len(tracks) == 2
|
||||
|
||||
# Limit a query
|
||||
tracks = await Track.objects.limit(1).all()
|
||||
assert len(tracks) == 1
|
||||
```
|
||||
|
||||
## Data types
|
||||
|
||||
The following keyword arguments are supported on all field types.
|
||||
|
||||
* `primary_key`
|
||||
* `nullable`
|
||||
* `default`
|
||||
* `server_default`
|
||||
* `index`
|
||||
* `unique`
|
||||
|
||||
## Model Fields
|
||||
|
||||
### Common parameters
|
||||
|
||||
All fields are required unless one of the following is set:
|
||||
|
||||
* `nullable` - Creates a nullable column. Sets the default to `None`.
|
||||
* `default` - Set a default value for the field.
|
||||
* `server_default` - Set a default value for the field on server side (like sqlalchemy's `func.now()`).
|
||||
* `primary key` - Set a primary key on a column.
|
||||
* `autoincrement` - When a column is set to primary key and autoincrement is set on this column.
|
||||
Autoincrement is set by default on int primary keys.
|
||||
|
||||
### Fields Types
|
||||
|
||||
* `String(length)`
|
||||
* `Text()`
|
||||
* `Boolean()`
|
||||
* `Integer()`
|
||||
* `Float()`
|
||||
* `Date()`
|
||||
* `Time()`
|
||||
* `DateTime()`
|
||||
* `JSON()`
|
||||
* `BigInteger()`
|
||||
* `Decimal(lenght, precision)`
|
||||
|
||||
[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
|
||||
[databases]: https://github.com/encode/databases
|
||||
[pydantic]: https://pydantic-docs.helpmanual.io/
|
||||
[encode/orm]: https://github.com/encode/orm/
|
||||
[alembic]: https://alembic.sqlalchemy.org/en/latest/
|
||||
[fastapi]: https://fastapi.tiangolo.com/
|
||||
179
docs/models.md
Normal file
179
docs/models.md
Normal file
@ -0,0 +1,179 @@
|
||||
# Models
|
||||
|
||||
## Defining models
|
||||
|
||||
By defining an ormar Model you get corresponding **Pydantic model** as well as **Sqlalchemy table** for free.
|
||||
They are being managed in the background and you do not have to create them on your own.
|
||||
|
||||
### Model Class
|
||||
|
||||
To build an ormar model you simply need to inherit a `ormar.Model` class.
|
||||
|
||||
```Python hl_lines="10"
|
||||
--8<-- "../docs_src/models/docs001.py"
|
||||
```
|
||||
|
||||
### Defining Fields
|
||||
|
||||
Next assign one or more of the [Fields][fields] as a class level variables.
|
||||
|
||||
Each table **has to** have a primary key column, which you specify by setting `primary_key=True` on selected field.
|
||||
|
||||
Only one primary key column is allowed.
|
||||
|
||||
```Python hl_lines="14 15 16"
|
||||
--8<-- "../docs_src/models/docs001.py"
|
||||
```
|
||||
|
||||
!!! warning
|
||||
Not assigning `primary_key` column or assigning more than one column per `Model` will raise `ModelDefinitionError`
|
||||
exception.
|
||||
|
||||
By default if you assign primary key to `Integer` field, the `autoincrement` option is set to true.
|
||||
|
||||
You can disable by passing `autoincremant=False`.
|
||||
|
||||
```Python
|
||||
id = ormar.Integer(primary_key=True, autoincrement=False)
|
||||
```
|
||||
|
||||
Names of the fields will be used for both the underlying `pydantic` model and `sqlalchemy` table.
|
||||
|
||||
### Dependencies
|
||||
|
||||
Since ormar depends on [`databases`][databases] and [`sqlalchemy-core`][sqlalchemy-core] for database connection
|
||||
and table creation you need to assign each `Model` with two special parameters.
|
||||
|
||||
#### Databases
|
||||
|
||||
One is `Database` instance created with your database url in [sqlalchemy connection string][sqlalchemy connection string] format.
|
||||
|
||||
Created instance needs to be passed to every `Model` with `__database__` parameter.
|
||||
|
||||
```Python hl_lines="1 6 11"
|
||||
--8<-- "../docs_src/models/docs001.py"
|
||||
```
|
||||
|
||||
!!! tip
|
||||
You need to create the `Database` instance **only once** and use it for all models.
|
||||
You can create several ones if you want to use multiple databases.
|
||||
|
||||
#### Sqlalchemy
|
||||
|
||||
Second dependency is sqlalchemy `MetaData` instance.
|
||||
|
||||
Created instance needs to be passed to every `Model` with `__metadata__` parameter.
|
||||
|
||||
```Python hl_lines="2 7 12"
|
||||
--8<-- "../docs_src/models/docs001.py"
|
||||
```
|
||||
|
||||
!!! tip
|
||||
You need to create the `MetaData` instance **only once** and use it for all models.
|
||||
You can create several ones if you want to use multiple databases.
|
||||
|
||||
### Table Names
|
||||
|
||||
By default table name is created from Model class name as lowercase name plus 's'.
|
||||
|
||||
You can overwrite this parameter by providing `__tablename__` argument.
|
||||
|
||||
```Python hl_lines="11 12 13"
|
||||
--8<-- "../docs_src/models/docs002.py"
|
||||
```
|
||||
|
||||
## Initialization
|
||||
|
||||
There are two ways to create and persist the `Model` instance in the database.
|
||||
|
||||
!!!tip
|
||||
Use `ipython` to try this from the console, since it supports `await`.
|
||||
|
||||
If you plan to modify the instance in the later execution of your program you can initiate your `Model` as a normal class and later await a `save()` call.
|
||||
|
||||
```Python hl_lines="19 20"
|
||||
--8<-- "../docs_src/models/docs007.py"
|
||||
```
|
||||
|
||||
If you want to initiate your `Model` and at the same time save in in the database use a QuerySet's method `create()`.
|
||||
|
||||
Each model has a `QuerySet` initialised as `objects` parameter
|
||||
|
||||
```Python hl_lines="22"
|
||||
--8<-- "../docs_src/models/docs007.py"
|
||||
```
|
||||
|
||||
!!!info
|
||||
To read more about `QuerySets` and available methods visit [queries][queries]
|
||||
|
||||
## Attributes Delegation
|
||||
|
||||
Each call to `Model` fields parameter under the hood is delegated to either the `pydantic` model
|
||||
or other related `Model` in case of relations.
|
||||
|
||||
The fields and relations are not stored on the `Model` itself
|
||||
|
||||
```Python hl_lines="31 32 33 34 35 36 37 38 39 40 41"
|
||||
--8<-- "../docs_src/models/docs006.py"
|
||||
```
|
||||
|
||||
!!! warning
|
||||
In example above model instances are created but not persisted that's why `id` of `department` is None!
|
||||
|
||||
!!!info
|
||||
To read more about `ForeignKeys` and `Model` relations visit [relations][relations]
|
||||
|
||||
## Internals
|
||||
|
||||
Apart from special parameters defined in the `Model` during definition (tablename, metadata etc.) the `Model` provides you with useful internals.
|
||||
|
||||
### Pydantic Model
|
||||
|
||||
To access auto created pydantic model you can use `Model.__pydantic_model__` parameter
|
||||
|
||||
For example to list model fields you can:
|
||||
|
||||
```Python hl_lines="18"
|
||||
--8<-- "../docs_src/models/docs003.py"
|
||||
```
|
||||
|
||||
!!!tip
|
||||
Note how the primary key `id` field is optional as `Integer` primary key by default has `autoincrement` set to `True`.
|
||||
|
||||
!!!info
|
||||
For more options visit official [pydantic][pydantic] documentation.
|
||||
|
||||
### Sqlalchemy Table
|
||||
|
||||
To access auto created sqlalchemy table you can use `Model.__table__` parameter
|
||||
|
||||
For example to list table columns you can:
|
||||
|
||||
```Python hl_lines="18"
|
||||
--8<-- "../docs_src/models/docs004.py"
|
||||
```
|
||||
|
||||
!!!tip
|
||||
You can access table primary key name by `Course.__pkname__`
|
||||
|
||||
!!!info
|
||||
For more options visit official [sqlalchemy-metadata][sqlalchemy-metadata] documentation.
|
||||
|
||||
### Fields Definition
|
||||
|
||||
To access ormar `Fields` you can use `Model.__model_fields__` parameter
|
||||
|
||||
For example to list table model fields you can:
|
||||
|
||||
```Python hl_lines="18"
|
||||
--8<-- "../docs_src/models/docs005.py"
|
||||
```
|
||||
|
||||
[fields]: ./fields.md
|
||||
[relations]: ./relations.md
|
||||
[queries]: ./queries.md
|
||||
[pydantic]: https://pydantic-docs.helpmanual.io/
|
||||
[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
|
||||
[sqlalchemy-metadata]: https://docs.sqlalchemy.org/en/13/core/metadata.html
|
||||
[databases]: https://github.com/encode/databases
|
||||
[sqlalchemy connection string]: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
|
||||
0
docs/pydantic.md
Normal file
0
docs/pydantic.md
Normal file
156
docs/queries.md
Normal file
156
docs/queries.md
Normal file
@ -0,0 +1,156 @@
|
||||
# Queries
|
||||
|
||||
## QuerySet
|
||||
|
||||
Each Model is auto registered with a QuerySet that represents the underlaying query and it's options.
|
||||
|
||||
Given the Models like this
|
||||
|
||||
```Python
|
||||
--8<-- "../docs_src/relations/docs001.py"
|
||||
```
|
||||
|
||||
we can demonstrate available methods to fetch and save the data into the database.
|
||||
|
||||
### create(**kwargs)
|
||||
|
||||
Creates the model instance, saves it in a database and returns the updates model (with pk populated).
|
||||
The allowed kwargs are `Model` fields names and proper value types.
|
||||
|
||||
```python
|
||||
malibu = await Album.objects.create(name="Malibu")
|
||||
await Track.objects.create(album=malibu, title="The Bird", position=1)
|
||||
```
|
||||
|
||||
The alternative is a split creation and persistence of the `Model`.
|
||||
```python
|
||||
malibu = Album(name="Malibu")
|
||||
await malibu.save()
|
||||
```
|
||||
|
||||
### load()
|
||||
|
||||
By default when you query a table without prefetching related models, the ormar will still construct
|
||||
your related models, but populate them only with the pk value.
|
||||
|
||||
```python
|
||||
track = await Track.objects.get(name='The Bird')
|
||||
track.album.pk # will return malibu album pk (1)
|
||||
track.album.name # will return None
|
||||
|
||||
# you need to actually load the data first
|
||||
await track.album.load()
|
||||
track.album.name # will return 'Malibu'
|
||||
```
|
||||
|
||||
### get(**kwargs)
|
||||
|
||||
Get's the first row from the db meeting the criteria set by kwargs.
|
||||
|
||||
If no criteria set it will return the first row in db.
|
||||
|
||||
Passing a criteria is actually calling filter(**kwargs) method described below.
|
||||
|
||||
```python
|
||||
track = await Track.objects.get(name='The Bird')
|
||||
track2 = track = await Track.objects.get()
|
||||
track == track2 # True since it's the only row in db
|
||||
```
|
||||
|
||||
### all()
|
||||
|
||||
Returns all rows from a database for given model
|
||||
|
||||
```python
|
||||
tracks = await Track.objects.select_related("album").all()
|
||||
# will return a list of all Tracks
|
||||
```
|
||||
|
||||
### filter(**kwargs)
|
||||
|
||||
Allows you to filter by any `Model` attribute/field
|
||||
as well as to fetch instances, with a filter across an FK relationship.
|
||||
|
||||
```python
|
||||
track = Track.objects.filter(name="The Bird").get()
|
||||
# will return a track with name equal to 'The Bird'
|
||||
|
||||
tracks = Track.objects.filter(album__name="Fantasies").all()
|
||||
# will return all tracks where the related album name = 'Fantasies'
|
||||
```
|
||||
|
||||
You can use special filter suffix to change the filter operands:
|
||||
|
||||
* exact - like `album__name__exact='Malibu'` (exact match)
|
||||
* iexact - like `album__name__iexact='malibu'` (exact match case insensitive)
|
||||
* contains - like `album__name__conatins='Mal'` (sql like)
|
||||
* icontains - like `album__name__icontains='mal'` (sql like case insensitive)
|
||||
* in - like `album__name__in=['Malibu', 'Barclay']` (sql in)
|
||||
* gt - like `position__gt=3` (sql >)
|
||||
* gte - like `position__gte=3` (sql >=)
|
||||
* lt - like `position__lt=3` (sql <)
|
||||
* lte - like `position__lte=3` (sql <=)
|
||||
|
||||
!!!note
|
||||
`filter()`, `select_related()`, `limit()` and `offset()` returns a QueySet instance so you can chain them together.
|
||||
|
||||
Something like `Track.object.select_related("album").filter(album__name="Malibu").offset(1).limit(1).all()`
|
||||
|
||||
### select_related(*args)
|
||||
|
||||
Allows to prefetch related models.
|
||||
|
||||
To fetch related model use `ForeignKey` names.
|
||||
|
||||
To chain related `Models` relation use double underscore.
|
||||
|
||||
```python
|
||||
album = await Album.objects.select_related("tracks").all()
|
||||
# will return album will all related tracks
|
||||
```
|
||||
|
||||
You can provide a string or a list of strings
|
||||
|
||||
```python
|
||||
classes = await SchoolClass.objects.select_related(
|
||||
["teachers__category", "students"]).all()
|
||||
# will return classes with teachers and teachers categories
|
||||
# as well as classes students
|
||||
```
|
||||
|
||||
!!!warning
|
||||
If you set `ForeignKey` field as not nullable (so required) during
|
||||
all queries the not nullable `Models` will be auto prefetched, even if you do not include them in select_related.
|
||||
|
||||
!!!note
|
||||
`filter()`, `select_related()`, `limit()` and `offset()` returns a QueySet instance so you can chain them together.
|
||||
|
||||
Something like `Track.object.select_related("album").filter(album__name="Malibu").offset(1).limit(1).all()`
|
||||
|
||||
### limit(int)
|
||||
|
||||
You can limit the results to desired number of rows.
|
||||
|
||||
```python
|
||||
tracks = await Track.objects.limit(1).all()
|
||||
# will return just one Track
|
||||
```
|
||||
|
||||
!!!note
|
||||
`filter()`, `select_related()`, `limit()` and `offset()` returns a QueySet instance so you can chain them together.
|
||||
|
||||
Something like `Track.object.select_related("album").filter(album__name="Malibu").offset(1).limit(1).all()`
|
||||
|
||||
### offset(int)
|
||||
|
||||
You can also offset the results by desired number of rows.
|
||||
|
||||
```python
|
||||
tracks = await Track.objects.offset(1).limit(1).all()
|
||||
# will return just one Track, but this time the second one
|
||||
```
|
||||
|
||||
!!!note
|
||||
`filter()`, `select_related()`, `limit()` and `offset()` returns a QueySet instance so you can chain them together.
|
||||
|
||||
Something like `Track.object.select_related("album").filter(album__name="Malibu").offset(1).limit(1).all()`
|
||||
206
docs/relations.md
Normal file
206
docs/relations.md
Normal file
@ -0,0 +1,206 @@
|
||||
# Relations
|
||||
|
||||
## Defining a relationship
|
||||
|
||||
### Foreign Key
|
||||
|
||||
To define a relationship you simply need to create a ForeignKey field on one `Model` and point it to another `Model`.
|
||||
|
||||
```Python hl_lines="24"
|
||||
--8<-- "../docs_src/relations/docs001.py"
|
||||
```
|
||||
|
||||
It automatically creates an sql foreign key constraint on a underlying table as well as nested pydantic model in the definition.
|
||||
|
||||
|
||||
```Python hl_lines="29 33"
|
||||
--8<-- "../docs_src/relations/docs002.py"
|
||||
```
|
||||
|
||||
Of course it's handled for you so you don't have to delve deep into this but you can.
|
||||
|
||||
!!!tip
|
||||
Note how by default the relation is optional, you can require the related `Model` by setting `nullable=False` on the `ForeignKey` field.
|
||||
|
||||
### Reverse Relation
|
||||
|
||||
At the same time the reverse relationship is registered automatically on parent model (target of `ForeignKey`).
|
||||
|
||||
By default it's child (source) `Model` name + 's', like courses in snippet below:
|
||||
|
||||
```Python hl_lines="25 31"
|
||||
--8<-- "../docs_src/fields/docs001.py"
|
||||
```
|
||||
|
||||
But you can overwrite this name by providing `related_name` parameter like below:
|
||||
|
||||
```Python hl_lines="25 30"
|
||||
--8<-- "../docs_src/fields/docs002.py"
|
||||
```
|
||||
|
||||
!!!tip
|
||||
Since related models are coming from Relationship Manager the reverse relation on access returns list of `wekref.proxy` to avoid circular references.
|
||||
|
||||
## Relationship Manager
|
||||
|
||||
!!!tip
|
||||
This section is more technical so you might want to skip it if you are not interested in implementation details.
|
||||
|
||||
### Need for a manager?
|
||||
|
||||
Since orm uses Sqlalchemy core under the hood to prepare the queries,
|
||||
the orm needs a way to uniquely identify each relationship between the tables to construct working queries.
|
||||
|
||||
Imagine that you have models as following:
|
||||
|
||||
```Python
|
||||
--8<-- "../docs_src/relations/docs003.py"
|
||||
```
|
||||
|
||||
Now imagine that you want to go from school class to student and his category and to teacher and his category.
|
||||
|
||||
```Python
|
||||
classes = await SchoolClass.objects.select_related(
|
||||
["teachers__category", "students__category"]).all()
|
||||
```
|
||||
|
||||
!!!tip
|
||||
To query a chain of models use double underscores between the relation names (`ForeignKeys` or reverse `ForeignKeys`)
|
||||
|
||||
!!!note
|
||||
To select related models use `select_related` method from `Model` `QuerySet`.
|
||||
|
||||
Note that you use relation (`ForeignKey`) names and not the table names.
|
||||
|
||||
Since you join two times to the same table (categories) it won't work by default -> you would need to use aliases for category tables and columns.
|
||||
|
||||
But don't worry - ormar can handle situations like this, as it uses the Relationship Manager which has it's aliases defined for all relationships.
|
||||
|
||||
Each class is registered with the same instance of the RelationshipManager that you can access like this:
|
||||
|
||||
```python
|
||||
SchoolClass._orm_relationship_manager
|
||||
```
|
||||
|
||||
It's the same object for all `Models`
|
||||
|
||||
```python
|
||||
print(Teacher._orm_relationship_manager == Student._orm_relationship_manager)
|
||||
# will produce: True
|
||||
```
|
||||
|
||||
### Table aliases
|
||||
|
||||
You can even preview the alias used for any relation by passing two tables names.
|
||||
|
||||
```python
|
||||
print(Teacher._orm_relationship_manager.resolve_relation_join(
|
||||
'students', 'categories'))
|
||||
# will produce: KId1c6 (sample value)
|
||||
|
||||
print(Teacher._orm_relationship_manager.resolve_relation_join(
|
||||
'categories', 'students'))
|
||||
# will produce: EFccd5 (sample value)
|
||||
```
|
||||
|
||||
!!!note
|
||||
The order that you pass the names matters -> as those are 2 different relationships depending on join order.
|
||||
|
||||
As aliases are produced randomly you can be presented with different results.
|
||||
|
||||
### Query automatic construction
|
||||
|
||||
Ormar is using those aliases during queries to both construct a meaningful and valid sql,
|
||||
as well as later use it to extract proper columns for proper nested models.
|
||||
|
||||
Running a previously mentioned query to select school classes and related teachers and students:
|
||||
|
||||
```Python
|
||||
classes = await SchoolClass.objects.select_related(
|
||||
["teachers__category", "students__category"]).all()
|
||||
```
|
||||
|
||||
Will result in a query like this (run under the hood):
|
||||
|
||||
```sql
|
||||
SELECT schoolclasses.id,
|
||||
schoolclasses.name,
|
||||
schoolclasses.department,
|
||||
NZc8e2_students.id as NZc8e2_id,
|
||||
NZc8e2_students.name as NZc8e2_name,
|
||||
NZc8e2_students.schoolclass as NZc8e2_schoolclass,
|
||||
NZc8e2_students.category as NZc8e2_category,
|
||||
MYfe53_categories.id as MYfe53_id,
|
||||
MYfe53_categories.name as MYfe53_name,
|
||||
WA49a3_teachers.id as WA49a3_id,
|
||||
WA49a3_teachers.name as WA49a3_name,
|
||||
WA49a3_teachers.schoolclass as WA49a3_schoolclass,
|
||||
WA49a3_teachers.category as WA49a3_category,
|
||||
WZa13b_categories.id as WZa13b_id,
|
||||
WZa13b_categories.name as WZa13b_name
|
||||
FROM schoolclasses
|
||||
LEFT OUTER JOIN students NZc8e2_students ON NZc8e2_students.schoolclass = schoolclasses.id
|
||||
LEFT OUTER JOIN categories MYfe53_categories ON MYfe53_categories.id = NZc8e2_students.category
|
||||
LEFT OUTER JOIN teachers WA49a3_teachers ON WA49a3_teachers.schoolclass = schoolclasses.id
|
||||
LEFT OUTER JOIN categories WZa13b_categories ON WZa13b_categories.id = WA49a3_teachers.category
|
||||
ORDER BY schoolclasses.id, NZc8e2_students.id, MYfe53_categories.id, WA49a3_teachers.id, WZa13b_categories.id
|
||||
```
|
||||
|
||||
!!!note
|
||||
As mentioned before the aliases are produced dynamically so the actual result might differ.
|
||||
|
||||
Note that aliases are assigned to relations and not the tables, therefore the first table is always without an alias.
|
||||
|
||||
### Returning related Models
|
||||
|
||||
Each object in Relationship Manager is identified by orm_id which you can preview like this
|
||||
|
||||
```python
|
||||
category = Category(name='Math')
|
||||
print(category._orm_id)
|
||||
# will produce: c76046d9410c4582a656bf12a44c892c (sample value)
|
||||
```
|
||||
|
||||
Each call to related `Model` is actually coming through the Manager which stores all
|
||||
the relations in a dictionary and returns related `Models` by relation type (name) and by object _orm_id.
|
||||
|
||||
Since we register both sides of the relation the side registering the relation
|
||||
is always registering the other side as concrete model,
|
||||
while the reverse relation is a weakref.proxy to avoid circular references.
|
||||
|
||||
Sounds complicated but in reality it means something like this:
|
||||
|
||||
```python
|
||||
test_class = await SchoolClass.objects.create(name='Test')
|
||||
student = await Student.objects.create(name='John', schoolclass=test_class)
|
||||
# the relation to schoolsclass from student (i.e. when you call student.schoolclass)
|
||||
# is a concrete one, meaning directy relating the schoolclass `Model` object
|
||||
# On the other side calling test_class.students will result in a list of wekref.proxy objects
|
||||
```
|
||||
|
||||
!!!tip
|
||||
To learn more about queries and available methods please review [queries][queries] section.
|
||||
|
||||
All relations are kept in lists, meaning that when you access related object the Relationship Manager is
|
||||
searching itself for related models and get a list of them.
|
||||
|
||||
But since child to parent relation is a many to one type,
|
||||
the Manager is unpacking the first (and only) related model from a list and you get an actual `Model` instance instead of a list.
|
||||
|
||||
Coming from parent to child relation (one to many) you always get a list of results.
|
||||
|
||||
Translating this into concrete sample, the same as above:
|
||||
|
||||
```python
|
||||
test_class = await SchoolClass.objects.create(name='Test')
|
||||
student = await Student.objects.create(name='John', schoolclass=test_class)
|
||||
|
||||
student.schoolclass # return a test_class instance extracted from relationship list
|
||||
test_class.students # return a list of related wekref.proxy refering related students `Models`
|
||||
|
||||
```
|
||||
|
||||
!!!tip
|
||||
You can preview all relations currently registered by accessing Relationship Manager on any class/instance `Student._orm_relationship_manager._relations`
|
||||
|
||||
[queries]: ./queries.md
|
||||
Reference in New Issue
Block a user