switch to equals in most of the code, fix dependencies, clean tests, make all not relation fields work with type hints
This commit is contained in:
12
README.md
12
README.md
@ -75,8 +75,8 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(length=100)
|
name = ormar.String(length=100)
|
||||||
|
|
||||||
|
|
||||||
class Track(ormar.Model):
|
class Track(ormar.Model):
|
||||||
@ -85,10 +85,10 @@ class Track(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
album: ormar.ForeignKey(Album)
|
album= ormar.ForeignKey(Album)
|
||||||
title: ormar.String(length=100)
|
title = ormar.String(length=100)
|
||||||
position: ormar.Integer()
|
position = ormar.Integer()
|
||||||
|
|
||||||
|
|
||||||
# Create some records to work with.
|
# Create some records to work with.
|
||||||
|
|||||||
@ -75,8 +75,8 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(length=100)
|
name = ormar.String(length=100)
|
||||||
|
|
||||||
|
|
||||||
class Track(ormar.Model):
|
class Track(ormar.Model):
|
||||||
@ -85,10 +85,10 @@ class Track(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
album: ormar.ForeignKey(Album)
|
album: Optional[Album] =ormar.ForeignKey(Album)
|
||||||
title: ormar.String(length=100)
|
title = ormar.String(length=100)
|
||||||
position: ormar.Integer()
|
position = ormar.Integer()
|
||||||
|
|
||||||
|
|
||||||
# Create some records to work with.
|
# Create some records to work with.
|
||||||
|
|||||||
@ -34,7 +34,7 @@ By default if you assign primary key to `Integer` field, the `autoincrement` opt
|
|||||||
You can disable by passing `autoincremant=False`.
|
You can disable by passing `autoincremant=False`.
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
id: ormar.Integer(primary_key=True, autoincrement=False)
|
id = ormar.Integer(primary_key=True, autoincrement=False)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Fields names vs Column names
|
### Fields names vs Column names
|
||||||
|
|||||||
@ -109,10 +109,10 @@ class Book(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
title: ormar.String(max_length=200)
|
title = ormar.String(max_length=200)
|
||||||
author: ormar.String(max_length=100)
|
author = ormar.String(max_length=100)
|
||||||
genre: ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
genre = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
||||||
|
|
||||||
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
||||||
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
|
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
|
||||||
@ -146,10 +146,10 @@ class Book(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
title: ormar.String(max_length=200)
|
title = ormar.String(max_length=200)
|
||||||
author: ormar.String(max_length=100)
|
author = ormar.String(max_length=100)
|
||||||
genre: ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
genre = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
||||||
|
|
||||||
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
||||||
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
|
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
|
||||||
@ -192,9 +192,9 @@ class ToDo(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
text: ormar.String(max_length=500)
|
text = ormar.String(max_length=500)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|
||||||
# create multiple instances at once with bulk_create
|
# create multiple instances at once with bulk_create
|
||||||
await ToDo.objects.bulk_create(
|
await ToDo.objects.bulk_create(
|
||||||
@ -259,10 +259,10 @@ class Book(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
title: ormar.String(max_length=200)
|
title = ormar.String(max_length=200)
|
||||||
author: ormar.String(max_length=100)
|
author = ormar.String(max_length=100)
|
||||||
genre: ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
genre = ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
||||||
|
|
||||||
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
||||||
await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
|
await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
|
||||||
@ -470,9 +470,9 @@ class Company(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
founded: ormar.Integer(nullable=True)
|
founded = ormar.Integer(nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class Car(ormar.Model):
|
class Car(ormar.Model):
|
||||||
@ -481,13 +481,13 @@ class Car(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
manufacturer: ormar.ForeignKey(Company)
|
manufacturer= ormar.ForeignKey(Company)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
year: ormar.Integer(nullable=True)
|
year = ormar.Integer(nullable=True)
|
||||||
gearbox_type: ormar.String(max_length=20, nullable=True)
|
gearbox_type = ormar.String(max_length=20, nullable=True)
|
||||||
gears: ormar.Integer(nullable=True)
|
gears = ormar.Integer(nullable=True)
|
||||||
aircon_type: ormar.String(max_length=20, nullable=True)
|
aircon_type = ormar.String(max_length=20, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
@ -32,8 +32,8 @@ class Category(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Item(ormar.Model):
|
class Item(ormar.Model):
|
||||||
@ -42,9 +42,9 @@ class Item(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category= ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/items/", response_model=List[Item])
|
@app.get("/items/", response_model=List[Item])
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
@ -12,8 +14,8 @@ class Department(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Course(ormar.Model):
|
class Course(ormar.Model):
|
||||||
@ -21,10 +23,10 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
department: ormar.ForeignKey(Department)
|
department= ormar.ForeignKey(Department)
|
||||||
|
|
||||||
|
|
||||||
department = Department(name='Science')
|
department = Department(name='Science')
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
@ -12,8 +14,8 @@ class Department(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Course(ormar.Model):
|
class Course(ormar.Model):
|
||||||
@ -21,10 +23,10 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
department: ormar.ForeignKey(Department, related_name="my_courses")
|
department= ormar.ForeignKey(Department, related_name="my_courses")
|
||||||
|
|
||||||
|
|
||||||
department = Department(name='Science')
|
department = Department(name='Science')
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
@ -12,8 +14,8 @@ class Department(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Course(ormar.Model):
|
class Course(ormar.Model):
|
||||||
@ -21,7 +23,7 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
department: ormar.ForeignKey(Department)
|
department= ormar.ForeignKey(Department)
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from sqlalchemy import func, text
|
from sqlalchemy import func, text
|
||||||
@ -14,8 +16,8 @@ class Product(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
company: ormar.String(max_length=200, server_default='Acme')
|
company = ormar.String(max_length=200, server_default='Acme')
|
||||||
sort_order: ormar.Integer(server_default=text("10"))
|
sort_order = ormar.Integer(server_default=text("10"))
|
||||||
created: ormar.DateTime(server_default=func.now())
|
created= ormar.DateTime(server_default=func.now())
|
||||||
|
|||||||
@ -12,6 +12,6 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|||||||
@ -15,6 +15,6 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|||||||
@ -12,9 +12,9 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|
||||||
|
|
||||||
print(Course.__fields__)
|
print(Course.__fields__)
|
||||||
|
|||||||
@ -12,9 +12,9 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|
||||||
|
|
||||||
print(Course.Meta.table.columns)
|
print(Course.Meta.table.columns)
|
||||||
|
|||||||
@ -12,9 +12,9 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|
||||||
print({x:v.__dict__ for x,v in Course.Meta.model_fields.items()})
|
print({x:v.__dict__ for x,v in Course.Meta.model_fields.items()})
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -13,7 +13,7 @@ class Child(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name='child_id', primary_key=True)
|
id = ormar.Integer(name='child_id', primary_key=True)
|
||||||
first_name: ormar.String(name='fname', max_length=100)
|
first_name = ormar.String(name='fname', max_length=100)
|
||||||
last_name: ormar.String(name='lname', max_length=100)
|
last_name = ormar.String(name='lname', max_length=100)
|
||||||
born_year: ormar.Integer(name='year_born', nullable=True)
|
born_year = ormar.Integer(name='year_born', nullable=True)
|
||||||
|
|||||||
@ -4,6 +4,6 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name='album_id', primary_key=True)
|
id = ormar.Integer(name='album_id', primary_key=True)
|
||||||
name: ormar.String(name='album_name', max_length=100)
|
name = ormar.String(name='album_name', max_length=100)
|
||||||
artist: ormar.ForeignKey(Artist, name='artist_id')
|
artist= ormar.ForeignKey(Artist, name='artist_id')
|
||||||
|
|||||||
@ -11,8 +11,8 @@ class Artist(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name='artist_id', primary_key=True)
|
id = ormar.Integer(name='artist_id', primary_key=True)
|
||||||
first_name: ormar.String(name='fname', max_length=100)
|
first_name = ormar.String(name='fname', max_length=100)
|
||||||
last_name: ormar.String(name='lname', max_length=100)
|
last_name = ormar.String(name='lname', max_length=100)
|
||||||
born_year: ormar.Integer(name='year')
|
born_year = ormar.Integer(name='year')
|
||||||
children: ormar.ManyToMany(Child, through=ArtistChildren)
|
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(Child, through=ArtistChildren)
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import ormar
|
import ormar
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
@ -12,8 +14,8 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Track(ormar.Model):
|
class Track(ormar.Model):
|
||||||
@ -22,7 +24,7 @@ class Track(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
album: ormar.ForeignKey(Album)
|
album= ormar.ForeignKey(Album)
|
||||||
title: ormar.String(max_length=100)
|
title = ormar.String(max_length=100)
|
||||||
position: ormar.Integer()
|
position = ormar.Integer()
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
@ -12,8 +14,8 @@ class Department(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Course(ormar.Model):
|
class Course(ormar.Model):
|
||||||
@ -21,10 +23,10 @@ class Course(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
department: ormar.ForeignKey(Department)
|
department= ormar.ForeignKey(Department)
|
||||||
|
|
||||||
|
|
||||||
department = Department(name='Science')
|
department = Department(name='Science')
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional, Union, List
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import ormar
|
import ormar
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
@ -12,9 +14,9 @@ class Author(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
first_name: ormar.String(max_length=80)
|
first_name = ormar.String(max_length=80)
|
||||||
last_name: ormar.String(max_length=80)
|
last_name = ormar.String(max_length=80)
|
||||||
|
|
||||||
|
|
||||||
class Category(ormar.Model):
|
class Category(ormar.Model):
|
||||||
@ -23,8 +25,8 @@ class Category(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=40)
|
name = ormar.String(max_length=40)
|
||||||
|
|
||||||
|
|
||||||
class PostCategory(ormar.Model):
|
class PostCategory(ormar.Model):
|
||||||
@ -42,7 +44,7 @@ class Post(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
title: ormar.String(max_length=200)
|
title = ormar.String(max_length=200)
|
||||||
categories: ormar.ManyToMany(Category, through=PostCategory)
|
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(Category, through=PostCategory)
|
||||||
author: ormar.ForeignKey(Author)
|
author= ormar.ForeignKey(Author)
|
||||||
|
|||||||
3
mypy.ini
3
mypy.ini
@ -5,3 +5,6 @@ plugins = pydantic.mypy
|
|||||||
[mypy-sqlalchemy.*]
|
[mypy-sqlalchemy.*]
|
||||||
ignore_missing_imports = True
|
ignore_missing_imports = True
|
||||||
|
|
||||||
|
[mypy-tests.test_model_definition.*]
|
||||||
|
ignore_errors = True
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
|
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union, Sequence
|
||||||
|
|
||||||
from ormar.fields import BaseField
|
from ormar.fields import BaseField
|
||||||
from ormar.fields.foreign_key import ForeignKeyField
|
from ormar.fields.foreign_key import ForeignKeyField
|
||||||
@ -49,3 +49,58 @@ def ManyToMany(
|
|||||||
|
|
||||||
class ManyToManyField(ForeignKeyField):
|
class ManyToManyField(ForeignKeyField):
|
||||||
through: Type["Model"]
|
through: Type["Model"]
|
||||||
|
|
||||||
|
if TYPE_CHECKING: # pragma nocover
|
||||||
|
@staticmethod
|
||||||
|
async def add(item: "Model") -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def remove(item: "Model") -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
from ormar import QuerySet
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def filter(**kwargs: Any) -> "QuerySet": # noqa: A003
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def select_related(related: Union[List, str]) -> "QuerySet":
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def exists(self) -> bool:
|
||||||
|
return await self.queryset.exists()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def count(self) -> int:
|
||||||
|
return await self.queryset.count()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def clear(self) -> int:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def limit(limit_count: int) -> "QuerySet":
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def offset(self, offset: int) -> "QuerySet":
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def first(self, **kwargs: Any) -> "Model":
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get(self, **kwargs: Any) -> "Model":
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def all(self, **kwargs: Any) -> Sequence[Optional["Model"]]: # noqa: A003
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def create(self, **kwargs: Any) -> "Model":
|
||||||
|
pass
|
||||||
|
|||||||
@ -58,7 +58,7 @@ class ModelFieldFactory:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class String(ModelFieldFactory):
|
class String(ModelFieldFactory, str):
|
||||||
_type = str
|
_type = str
|
||||||
_pydantic_type = pydantic.ConstrainedStr
|
_pydantic_type = pydantic.ConstrainedStr
|
||||||
|
|
||||||
@ -97,7 +97,7 @@ class String(ModelFieldFactory):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Integer(ModelFieldFactory):
|
class Integer(ModelFieldFactory, int):
|
||||||
_type = int
|
_type = int
|
||||||
_pydantic_type = pydantic.ConstrainedInt
|
_pydantic_type = pydantic.ConstrainedInt
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ class Integer(ModelFieldFactory):
|
|||||||
return sqlalchemy.Integer()
|
return sqlalchemy.Integer()
|
||||||
|
|
||||||
|
|
||||||
class Text(ModelFieldFactory):
|
class Text(ModelFieldFactory, str):
|
||||||
_type = str
|
_type = str
|
||||||
_pydantic_type = pydantic.ConstrainedStr
|
_pydantic_type = pydantic.ConstrainedStr
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ class Text(ModelFieldFactory):
|
|||||||
return sqlalchemy.Text()
|
return sqlalchemy.Text()
|
||||||
|
|
||||||
|
|
||||||
class Float(ModelFieldFactory):
|
class Float(ModelFieldFactory, float):
|
||||||
_type = float
|
_type = float
|
||||||
_pydantic_type = pydantic.ConstrainedFloat
|
_pydantic_type = pydantic.ConstrainedFloat
|
||||||
|
|
||||||
@ -184,7 +184,7 @@ class Float(ModelFieldFactory):
|
|||||||
return sqlalchemy.Float()
|
return sqlalchemy.Float()
|
||||||
|
|
||||||
|
|
||||||
class Boolean(ModelFieldFactory):
|
class Boolean(ModelFieldFactory, int):
|
||||||
_type = bool
|
_type = bool
|
||||||
_pydantic_type = bool
|
_pydantic_type = bool
|
||||||
|
|
||||||
@ -193,7 +193,7 @@ class Boolean(ModelFieldFactory):
|
|||||||
return sqlalchemy.Boolean()
|
return sqlalchemy.Boolean()
|
||||||
|
|
||||||
|
|
||||||
class DateTime(ModelFieldFactory):
|
class DateTime(ModelFieldFactory, datetime.datetime):
|
||||||
_type = datetime.datetime
|
_type = datetime.datetime
|
||||||
_pydantic_type = datetime.datetime
|
_pydantic_type = datetime.datetime
|
||||||
|
|
||||||
@ -202,7 +202,7 @@ class DateTime(ModelFieldFactory):
|
|||||||
return sqlalchemy.DateTime()
|
return sqlalchemy.DateTime()
|
||||||
|
|
||||||
|
|
||||||
class Date(ModelFieldFactory):
|
class Date(ModelFieldFactory, datetime.date):
|
||||||
_type = datetime.date
|
_type = datetime.date
|
||||||
_pydantic_type = datetime.date
|
_pydantic_type = datetime.date
|
||||||
|
|
||||||
@ -211,7 +211,7 @@ class Date(ModelFieldFactory):
|
|||||||
return sqlalchemy.Date()
|
return sqlalchemy.Date()
|
||||||
|
|
||||||
|
|
||||||
class Time(ModelFieldFactory):
|
class Time(ModelFieldFactory, datetime.time):
|
||||||
_type = datetime.time
|
_type = datetime.time
|
||||||
_pydantic_type = datetime.time
|
_pydantic_type = datetime.time
|
||||||
|
|
||||||
@ -220,7 +220,7 @@ class Time(ModelFieldFactory):
|
|||||||
return sqlalchemy.Time()
|
return sqlalchemy.Time()
|
||||||
|
|
||||||
|
|
||||||
class JSON(ModelFieldFactory):
|
class JSON(ModelFieldFactory, pydantic.Json):
|
||||||
_type = pydantic.Json
|
_type = pydantic.Json
|
||||||
_pydantic_type = pydantic.Json
|
_pydantic_type = pydantic.Json
|
||||||
|
|
||||||
@ -229,7 +229,7 @@ class JSON(ModelFieldFactory):
|
|||||||
return sqlalchemy.JSON()
|
return sqlalchemy.JSON()
|
||||||
|
|
||||||
|
|
||||||
class BigInteger(Integer):
|
class BigInteger(Integer, int):
|
||||||
_type = int
|
_type = int
|
||||||
_pydantic_type = pydantic.ConstrainedInt
|
_pydantic_type = pydantic.ConstrainedInt
|
||||||
|
|
||||||
@ -264,7 +264,7 @@ class BigInteger(Integer):
|
|||||||
return sqlalchemy.BigInteger()
|
return sqlalchemy.BigInteger()
|
||||||
|
|
||||||
|
|
||||||
class Decimal(ModelFieldFactory):
|
class Decimal(ModelFieldFactory, decimal.Decimal):
|
||||||
_type = decimal.Decimal
|
_type = decimal.Decimal
|
||||||
_pydantic_type = pydantic.ConstrainedDecimal
|
_pydantic_type = pydantic.ConstrainedDecimal
|
||||||
|
|
||||||
@ -319,7 +319,7 @@ class Decimal(ModelFieldFactory):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class UUID(ModelFieldFactory):
|
class UUID(ModelFieldFactory, uuid.UUID):
|
||||||
_type = uuid.UUID
|
_type = uuid.UUID
|
||||||
_pydantic_type = uuid.UUID
|
_pydantic_type = uuid.UUID
|
||||||
|
|
||||||
|
|||||||
@ -208,7 +208,9 @@ def extract_field_from_annotation_or_value(
|
|||||||
|
|
||||||
def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
|
def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
|
||||||
model_fields = {}
|
model_fields = {}
|
||||||
for field_name, field in attrs["__annotations__"].items():
|
potential_fields = {k: v for k, v in attrs["__annotations__"].items() if lenient_issubclass(v, BaseField)}
|
||||||
|
potential_fields.update({k: v for k, v in attrs.items() if lenient_issubclass(v, BaseField)})
|
||||||
|
for field_name, field in potential_fields.items():
|
||||||
# ormar fields can be used as annotation or as default value
|
# ormar fields can be used as annotation or as default value
|
||||||
if check_if_field_annotation_or_value_is_ormar(field, field_name, attrs):
|
if check_if_field_annotation_or_value_is_ormar(field, field_name, attrs):
|
||||||
ormar_field = extract_field_from_annotation_or_value(
|
ormar_field = extract_field_from_annotation_or_value(
|
||||||
@ -225,11 +227,10 @@ def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
|
|||||||
|
|
||||||
|
|
||||||
def extract_annotations_and_default_vals(
|
def extract_annotations_and_default_vals(
|
||||||
attrs: dict, bases: Tuple
|
attrs: dict
|
||||||
) -> Tuple[Dict, Dict]:
|
) -> Tuple[Dict, Dict]:
|
||||||
attrs["__annotations__"] = attrs.get("__annotations__") or bases[0].__dict__.get(
|
key = '__annotations__'
|
||||||
"__annotations__", {}
|
attrs[key] = attrs.get(key, {})
|
||||||
)
|
|
||||||
attrs, model_fields = populate_pydantic_default_values(attrs)
|
attrs, model_fields = populate_pydantic_default_values(attrs)
|
||||||
return attrs, model_fields
|
return attrs, model_fields
|
||||||
|
|
||||||
@ -276,7 +277,7 @@ def populate_meta_sqlalchemy_table_if_required(
|
|||||||
def get_pydantic_base_orm_config() -> Type[BaseConfig]:
|
def get_pydantic_base_orm_config() -> Type[BaseConfig]:
|
||||||
class Config(BaseConfig):
|
class Config(BaseConfig):
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
arbitrary_types_allowed = True
|
# arbitrary_types_allowed = True
|
||||||
|
|
||||||
return Config
|
return Config
|
||||||
|
|
||||||
@ -320,7 +321,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
|
|||||||
) -> "ModelMetaclass":
|
) -> "ModelMetaclass":
|
||||||
attrs["Config"] = get_pydantic_base_orm_config()
|
attrs["Config"] = get_pydantic_base_orm_config()
|
||||||
attrs["__name__"] = name
|
attrs["__name__"] = name
|
||||||
attrs, model_fields = extract_annotations_and_default_vals(attrs, bases)
|
attrs, model_fields = extract_annotations_and_default_vals(attrs)
|
||||||
new_model = super().__new__( # type: ignore
|
new_model = super().__new__( # type: ignore
|
||||||
mcs, name, bases, attrs
|
mcs, name, bases, attrs
|
||||||
)
|
)
|
||||||
|
|||||||
@ -24,6 +24,9 @@ def group_related_list(list_: List) -> Dict:
|
|||||||
return test_dict
|
return test_dict
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING: # pragma nocover
|
||||||
|
from ormar import QuerySet
|
||||||
|
|
||||||
T = TypeVar("T", bound="Model")
|
T = TypeVar("T", bound="Model")
|
||||||
|
|
||||||
|
|
||||||
@ -31,6 +34,7 @@ class Model(NewBaseModel):
|
|||||||
__abstract__ = False
|
__abstract__ = False
|
||||||
if TYPE_CHECKING: # pragma nocover
|
if TYPE_CHECKING: # pragma nocover
|
||||||
Meta: ModelMeta
|
Meta: ModelMeta
|
||||||
|
objects: "QuerySet"
|
||||||
|
|
||||||
def __repr__(self) -> str: # pragma nocover
|
def __repr__(self) -> str: # pragma nocover
|
||||||
attrs_to_include = ["tablename", "columns", "pkname"]
|
attrs_to_include = ["tablename", "columns", "pkname"]
|
||||||
|
|||||||
@ -136,7 +136,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
|||||||
|
|
||||||
def _extract_related_model_instead_of_field(
|
def _extract_related_model_instead_of_field(
|
||||||
self, item: str
|
self, item: str
|
||||||
) -> Optional[Union[T, Sequence[T]]]:
|
) -> Optional[Union["T", Sequence["T"]]]:
|
||||||
alias = self.get_column_alias(item)
|
alias = self.get_column_alias(item)
|
||||||
if alias in self._orm:
|
if alias in self._orm:
|
||||||
return self._orm.get(alias)
|
return self._orm.get(alias)
|
||||||
@ -173,7 +173,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
|||||||
def db_backend_name(cls) -> str:
|
def db_backend_name(cls) -> str:
|
||||||
return cls.Meta.database._backend._dialect.name
|
return cls.Meta.database._backend._dialect.name
|
||||||
|
|
||||||
def remove(self, name: T) -> None:
|
def remove(self, name: "T") -> None:
|
||||||
self._orm.remove_parent(self, name)
|
self._orm.remove_parent(self, name)
|
||||||
|
|
||||||
def dict( # noqa A003
|
def dict( # noqa A003
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class QuerySet:
|
|||||||
|
|
||||||
def __get__(
|
def __get__(
|
||||||
self,
|
self,
|
||||||
instance: Union["QuerySet", "QuerysetProxy"],
|
instance: Optional[Union["QuerySet", "QuerysetProxy"]],
|
||||||
owner: Union[Type["Model"], Type["QuerysetProxy"]],
|
owner: Union[Type["Model"], Type["QuerysetProxy"]],
|
||||||
) -> "QuerySet":
|
) -> "QuerySet":
|
||||||
if issubclass(owner, ormar.Model):
|
if issubclass(owner, ormar.Model):
|
||||||
|
|||||||
@ -28,28 +28,28 @@ class QuerysetProxy:
|
|||||||
def queryset(self, value: "QuerySet") -> None:
|
def queryset(self, value: "QuerySet") -> None:
|
||||||
self._queryset = value
|
self._queryset = value
|
||||||
|
|
||||||
def _assign_child_to_parent(self, child: Optional[T]) -> None:
|
def _assign_child_to_parent(self, child: Optional["T"]) -> None:
|
||||||
if child:
|
if child:
|
||||||
owner = self.relation._owner
|
owner = self.relation._owner
|
||||||
rel_name = owner.resolve_relation_name(owner, child)
|
rel_name = owner.resolve_relation_name(owner, child)
|
||||||
setattr(owner, rel_name, child)
|
setattr(owner, rel_name, child)
|
||||||
|
|
||||||
def _register_related(self, child: Union[T, Sequence[Optional[T]]]) -> None:
|
def _register_related(self, child: Union["T", Sequence[Optional["T"]]]) -> None:
|
||||||
if isinstance(child, list):
|
if isinstance(child, list):
|
||||||
for subchild in child:
|
for subchild in child:
|
||||||
self._assign_child_to_parent(subchild)
|
self._assign_child_to_parent(subchild)
|
||||||
else:
|
else:
|
||||||
assert isinstance(child, Model)
|
assert isinstance(child, ormar.Model)
|
||||||
self._assign_child_to_parent(child)
|
self._assign_child_to_parent(child)
|
||||||
|
|
||||||
async def create_through_instance(self, child: T) -> None:
|
async def create_through_instance(self, child: "T") -> None:
|
||||||
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
||||||
owner_column = self.relation._owner.get_name()
|
owner_column = self.relation._owner.get_name()
|
||||||
child_column = child.get_name()
|
child_column = child.get_name()
|
||||||
kwargs = {owner_column: self.relation._owner, child_column: child}
|
kwargs = {owner_column: self.relation._owner, child_column: child}
|
||||||
await queryset.create(**kwargs)
|
await queryset.create(**kwargs)
|
||||||
|
|
||||||
async def delete_through_instance(self, child: T) -> None:
|
async def delete_through_instance(self, child: "T") -> None:
|
||||||
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
||||||
owner_column = self.relation._owner.get_name()
|
owner_column = self.relation._owner.get_name()
|
||||||
child_column = child.get_name()
|
child_column = child.get_name()
|
||||||
|
|||||||
@ -25,15 +25,15 @@ class Relation:
|
|||||||
self,
|
self,
|
||||||
manager: "RelationsManager",
|
manager: "RelationsManager",
|
||||||
type_: RelationType,
|
type_: RelationType,
|
||||||
to: Type[T],
|
to: Type["T"],
|
||||||
through: Type[T] = None,
|
through: Type["T"] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.manager = manager
|
self.manager = manager
|
||||||
self._owner: "Model" = manager.owner
|
self._owner: "Model" = manager.owner
|
||||||
self._type: RelationType = type_
|
self._type: RelationType = type_
|
||||||
self.to: Type[T] = to
|
self.to: Type["T"] = to
|
||||||
self.through: Optional[Type[T]] = through
|
self.through: Optional[Type["T"]] = through
|
||||||
self.related_models: Optional[Union[RelationProxy, T]] = (
|
self.related_models: Optional[Union[RelationProxy, "T"]] = (
|
||||||
RelationProxy(relation=self)
|
RelationProxy(relation=self)
|
||||||
if type_ in (RelationType.REVERSE, RelationType.MULTIPLE)
|
if type_ in (RelationType.REVERSE, RelationType.MULTIPLE)
|
||||||
else None
|
else None
|
||||||
@ -52,7 +52,7 @@ class Relation:
|
|||||||
self.related_models.pop(ind)
|
self.related_models.pop(ind)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def add(self, child: T) -> None:
|
def add(self, child: "T") -> None:
|
||||||
relation_name = self._owner.resolve_relation_name(self._owner, child)
|
relation_name = self._owner.resolve_relation_name(self._owner, child)
|
||||||
if self._type == RelationType.PRIMARY:
|
if self._type == RelationType.PRIMARY:
|
||||||
self.related_models = child
|
self.related_models = child
|
||||||
@ -79,7 +79,7 @@ class Relation:
|
|||||||
self.related_models.pop(position) # type: ignore
|
self.related_models.pop(position) # type: ignore
|
||||||
del self._owner.__dict__[relation_name][position]
|
del self._owner.__dict__[relation_name][position]
|
||||||
|
|
||||||
def get(self) -> Optional[Union[List[T], T]]:
|
def get(self) -> Optional[Union[List["T"], "T"]]:
|
||||||
return self.related_models
|
return self.related_models
|
||||||
|
|
||||||
def __repr__(self) -> str: # pragma no cover
|
def __repr__(self) -> str: # pragma no cover
|
||||||
|
|||||||
@ -48,7 +48,7 @@ class RelationsManager:
|
|||||||
def __contains__(self, item: str) -> bool:
|
def __contains__(self, item: str) -> bool:
|
||||||
return item in self._related_names
|
return item in self._related_names
|
||||||
|
|
||||||
def get(self, name: str) -> Optional[Union[T, Sequence[T]]]:
|
def get(self, name: str) -> Optional[Union["T", Sequence["T"]]]:
|
||||||
relation = self._relations.get(name, None)
|
relation = self._relations.get(name, None)
|
||||||
if relation is not None:
|
if relation is not None:
|
||||||
return relation.get()
|
return relation.get()
|
||||||
|
|||||||
@ -15,10 +15,10 @@ class Child(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name="child_id", primary_key=True)
|
id = ormar.Integer(name="child_id", primary_key=True)
|
||||||
first_name: ormar.String(name="fname", max_length=100)
|
first_name = ormar.String(name="fname", max_length=100)
|
||||||
last_name: ormar.String(name="lname", max_length=100)
|
last_name = ormar.String(name="lname", max_length=100)
|
||||||
born_year: ormar.Integer(name="year_born", nullable=True)
|
born_year = ormar.Integer(name="year_born", nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class ArtistChildren(ormar.Model):
|
class ArtistChildren(ormar.Model):
|
||||||
@ -34,11 +34,13 @@ class Artist(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name="artist_id", primary_key=True)
|
id = ormar.Integer(name="artist_id", primary_key=True)
|
||||||
first_name: ormar.String(name="fname", max_length=100)
|
first_name = ormar.String(name="fname", max_length=100)
|
||||||
last_name: ormar.String(name="lname", max_length=100)
|
last_name = ormar.String(name="lname", max_length=100)
|
||||||
born_year: ormar.Integer(name="year")
|
born_year = ormar.Integer(name="year")
|
||||||
children: ormar.ManyToMany(Child, through=ArtistChildren)
|
children = ormar.ManyToMany(
|
||||||
|
Child, through=ArtistChildren
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Album(ormar.Model):
|
class Album(ormar.Model):
|
||||||
@ -47,9 +49,9 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name="album_id", primary_key=True)
|
id = ormar.Integer(name="album_id", primary_key=True)
|
||||||
name: ormar.String(name="album_name", max_length=100)
|
name = ormar.String(name="album_name", max_length=100)
|
||||||
artist: ormar.ForeignKey(Artist, name="artist_id")
|
artist = ormar.ForeignKey(Artist, name="artist_id")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import datetime
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
|
import pydantic
|
||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
@ -22,14 +23,14 @@ class Example(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=200, default="aaa")
|
name = ormar.String(max_length=200, default="aaa")
|
||||||
created: ormar.DateTime(default=datetime.datetime.now)
|
created = ormar.DateTime(default=datetime.datetime.now)
|
||||||
created_day: ormar.Date(default=datetime.date.today)
|
created_day = ormar.Date(default=datetime.date.today)
|
||||||
created_time: ormar.Time(default=time)
|
created_time = ormar.Time(default=time)
|
||||||
description: ormar.Text(nullable=True)
|
description = ormar.Text(nullable=True)
|
||||||
value: ormar.Float(nullable=True)
|
value = ormar.Float(nullable=True)
|
||||||
data: ormar.JSON(default={})
|
data = ormar.JSON(default={})
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -38,8 +38,8 @@ class Category(ormar.Model):
|
|||||||
class Meta(LocalMeta):
|
class Meta(LocalMeta):
|
||||||
tablename = "categories"
|
tablename = "categories"
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class ItemsXCategories(ormar.Model):
|
class ItemsXCategories(ormar.Model):
|
||||||
@ -51,9 +51,11 @@ class Item(ormar.Model):
|
|||||||
class Meta(LocalMeta):
|
class Meta(LocalMeta):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
categories: ormar.ManyToMany(Category, through=ItemsXCategories)
|
categories = ormar.ManyToMany(
|
||||||
|
Category, through=ItemsXCategories
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
@ -18,8 +20,8 @@ class Category(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Item(ormar.Model):
|
class Item(ormar.Model):
|
||||||
@ -28,9 +30,9 @@ class Item(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category = ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/items/", response_model=Item)
|
@app.post("/items/", response_model=Item)
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
@ -16,8 +18,8 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name: str = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Track(ormar.Model):
|
class Track(ormar.Model):
|
||||||
@ -26,10 +28,10 @@ class Track(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
album: ormar.ForeignKey(Album)
|
album: Optional[Album] = ormar.ForeignKey(Album)
|
||||||
title: ormar.String(max_length=100)
|
title: str = ormar.String(max_length=100)
|
||||||
position: ormar.Integer()
|
position: int = ormar.Integer()
|
||||||
|
|
||||||
|
|
||||||
class Cover(ormar.Model):
|
class Cover(ormar.Model):
|
||||||
@ -38,9 +40,9 @@ class Cover(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
album: ormar.ForeignKey(Album, related_name="cover_pictures")
|
album = ormar.ForeignKey(Album, related_name="cover_pictures")
|
||||||
title: ormar.String(max_length=100)
|
title = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Organisation(ormar.Model):
|
class Organisation(ormar.Model):
|
||||||
@ -49,8 +51,12 @@ class Organisation(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
ident: ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
|
ident = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
|
||||||
|
|
||||||
|
|
||||||
|
class Organization(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Team(ormar.Model):
|
class Team(ormar.Model):
|
||||||
@ -59,9 +65,9 @@ class Team(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
org: ormar.ForeignKey(Organisation)
|
org = ormar.ForeignKey(Organisation)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Member(ormar.Model):
|
class Member(ormar.Model):
|
||||||
@ -70,9 +76,9 @@ class Member(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
team: ormar.ForeignKey(Team)
|
team = ormar.ForeignKey(Team)
|
||||||
email: ormar.String(max_length=100)
|
email = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import List, Union, Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pytest
|
import pytest
|
||||||
@ -18,9 +19,9 @@ class Author(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
first_name: ormar.String(max_length=80)
|
first_name = ormar.String(max_length=80)
|
||||||
last_name: ormar.String(max_length=80)
|
last_name = ormar.String(max_length=80)
|
||||||
|
|
||||||
|
|
||||||
class Category(ormar.Model):
|
class Category(ormar.Model):
|
||||||
@ -29,8 +30,8 @@ class Category(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=40)
|
name = ormar.String(max_length=40)
|
||||||
|
|
||||||
|
|
||||||
class PostCategory(ormar.Model):
|
class PostCategory(ormar.Model):
|
||||||
@ -46,10 +47,12 @@ class Post(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
title: ormar.String(max_length=200)
|
title = ormar.String(max_length=200)
|
||||||
categories: ormar.ManyToMany(Category, through=PostCategory)
|
categories = ormar.ManyToMany(
|
||||||
author: ormar.ForeignKey(Author)
|
Category, through=PostCategory
|
||||||
|
)
|
||||||
|
author= ormar.ForeignKey(Author)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
|
|||||||
@ -1,13 +1,17 @@
|
|||||||
|
# type: ignore
|
||||||
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
import typing
|
||||||
|
|
||||||
import ormar.fields as fields
|
import ormar
|
||||||
from ormar.exceptions import ModelDefinitionError
|
from ormar.exceptions import ModelDefinitionError
|
||||||
from ormar.models import Model
|
from ormar.models import Model
|
||||||
|
from tests.settings import DATABASE_URL
|
||||||
|
|
||||||
metadata = sqlalchemy.MetaData()
|
metadata = sqlalchemy.MetaData()
|
||||||
|
|
||||||
@ -17,18 +21,18 @@ class ExampleModel(Model):
|
|||||||
tablename = "example"
|
tablename = "example"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
test: fields.Integer(primary_key=True)
|
test = ormar.Integer(primary_key=True)
|
||||||
test_string: fields.String(max_length=250)
|
test_string = ormar.String(max_length=250)
|
||||||
test_text: fields.Text(default="")
|
test_text = ormar.Text(default="")
|
||||||
test_bool: fields.Boolean(nullable=False)
|
test_bool = ormar.Boolean(nullable=False)
|
||||||
test_float: fields.Float() = None
|
test_float: ormar.Float() = None # type: ignore
|
||||||
test_datetime: fields.DateTime(default=datetime.datetime.now)
|
test_datetime = ormar.DateTime(default=datetime.datetime.now)
|
||||||
test_date: fields.Date(default=datetime.date.today)
|
test_date = ormar.Date(default=datetime.date.today)
|
||||||
test_time: fields.Time(default=datetime.time)
|
test_time = ormar.Time(default=datetime.time)
|
||||||
test_json: fields.JSON(default={})
|
test_json = ormar.JSON(default={})
|
||||||
test_bigint: fields.BigInteger(default=0)
|
test_bigint = ormar.BigInteger(default=0)
|
||||||
test_decimal: fields.Decimal(scale=10, precision=2)
|
test_decimal = ormar.Decimal(scale=10, precision=2)
|
||||||
test_decimal2: fields.Decimal(max_digits=10, decimal_places=2)
|
test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2)
|
||||||
|
|
||||||
|
|
||||||
fields_to_check = [
|
fields_to_check = [
|
||||||
@ -46,11 +50,26 @@ fields_to_check = [
|
|||||||
|
|
||||||
class ExampleModel2(Model):
|
class ExampleModel2(Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
tablename = "example2"
|
tablename = "examples"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
test: fields.Integer(primary_key=True)
|
test = ormar.Integer(primary_key=True)
|
||||||
test_string: fields.String(max_length=250)
|
test_string = ormar.String(max_length=250)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def event_loop():
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
yield loop
|
||||||
|
loop.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
async def create_test_database():
|
||||||
|
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||||
|
metadata.create_all(engine)
|
||||||
|
yield
|
||||||
|
metadata.drop_all(engine)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
@ -117,62 +136,64 @@ def test_sqlalchemy_table_is_created(example):
|
|||||||
assert all([field in example.Meta.table.columns for field in fields_to_check])
|
assert all([field in example.Meta.table.columns for field in fields_to_check])
|
||||||
|
|
||||||
|
|
||||||
def test_no_pk_in_model_definition():
|
@typing.no_type_check
|
||||||
with pytest.raises(ModelDefinitionError):
|
def test_no_pk_in_model_definition(): # type: ignore
|
||||||
|
with pytest.raises(ModelDefinitionError): # type: ignore
|
||||||
class ExampleModel2(Model):
|
class ExampleModel2(Model): # type: ignore
|
||||||
class Meta:
|
class Meta:
|
||||||
tablename = "example3"
|
tablename = "example2"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
test_string: fields.String(max_length=250)
|
test_string = ormar.String(max_length=250) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
@typing.no_type_check
|
||||||
def test_two_pks_in_model_definition():
|
def test_two_pks_in_model_definition():
|
||||||
with pytest.raises(ModelDefinitionError):
|
with pytest.raises(ModelDefinitionError):
|
||||||
|
@typing.no_type_check
|
||||||
class ExampleModel2(Model):
|
class ExampleModel2(Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
tablename = "example3"
|
tablename = "example3"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
id: fields.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
test_string: fields.String(max_length=250, primary_key=True)
|
test_string = ormar.String(max_length=250, primary_key=True)
|
||||||
|
|
||||||
|
|
||||||
|
@typing.no_type_check
|
||||||
def test_setting_pk_column_as_pydantic_only_in_model_definition():
|
def test_setting_pk_column_as_pydantic_only_in_model_definition():
|
||||||
with pytest.raises(ModelDefinitionError):
|
with pytest.raises(ModelDefinitionError):
|
||||||
|
|
||||||
class ExampleModel2(Model):
|
class ExampleModel2(Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
tablename = "example4"
|
tablename = "example4"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
test: fields.Integer(primary_key=True, pydantic_only=True)
|
test = ormar.Integer(primary_key=True, pydantic_only=True)
|
||||||
|
|
||||||
|
|
||||||
|
@typing.no_type_check
|
||||||
def test_decimal_error_in_model_definition():
|
def test_decimal_error_in_model_definition():
|
||||||
with pytest.raises(ModelDefinitionError):
|
with pytest.raises(ModelDefinitionError):
|
||||||
|
|
||||||
class ExampleModel2(Model):
|
class ExampleModel2(Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
tablename = "example5"
|
tablename = "example5"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
test: fields.Decimal(primary_key=True)
|
test = ormar.Decimal(primary_key=True)
|
||||||
|
|
||||||
|
|
||||||
|
@typing.no_type_check
|
||||||
def test_string_error_in_model_definition():
|
def test_string_error_in_model_definition():
|
||||||
with pytest.raises(ModelDefinitionError):
|
with pytest.raises(ModelDefinitionError):
|
||||||
|
|
||||||
class ExampleModel2(Model):
|
class ExampleModel2(Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
tablename = "example6"
|
tablename = "example6"
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
test: fields.String(primary_key=True)
|
test = ormar.String(primary_key=True)
|
||||||
|
|
||||||
|
|
||||||
|
@typing.no_type_check
|
||||||
def test_json_conversion_in_model():
|
def test_json_conversion_in_model():
|
||||||
with pytest.raises(pydantic.ValidationError):
|
with pytest.raises(pydantic.ValidationError):
|
||||||
ExampleModel(
|
ExampleModel(
|
||||||
|
|||||||
@ -22,8 +22,8 @@ class JsonSample(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
test_json: ormar.JSON(nullable=True)
|
test_json= ormar.JSON(nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class UUIDSample(ormar.Model):
|
class UUIDSample(ormar.Model):
|
||||||
@ -32,8 +32,8 @@ class UUIDSample(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.UUID(primary_key=True, default=uuid.uuid4)
|
id= ormar.UUID(primary_key=True, default=uuid.uuid4)
|
||||||
test_text: ormar.Text()
|
test_text = ormar.Text()
|
||||||
|
|
||||||
|
|
||||||
class User(ormar.Model):
|
class User(ormar.Model):
|
||||||
@ -42,8 +42,8 @@ class User(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100, default="")
|
name = ormar.String(max_length=100, default="")
|
||||||
|
|
||||||
|
|
||||||
class Product(ormar.Model):
|
class Product(ormar.Model):
|
||||||
@ -52,11 +52,11 @@ class Product(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
rating: ormar.Integer(minimum=1, maximum=5)
|
rating = ormar.Integer(minimum=1, maximum=5)
|
||||||
in_stock: ormar.Boolean(default=False)
|
in_stock= ormar.Boolean(default=False)
|
||||||
last_delivery: ormar.Date(default=datetime.now)
|
last_delivery= ormar.Date(default=datetime.now)
|
||||||
|
|
||||||
|
|
||||||
country_name_choices = ("Canada", "Algeria", "United States")
|
country_name_choices = ("Canada", "Algeria", "United States")
|
||||||
@ -70,12 +70,12 @@ class Country(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(
|
name = ormar.String(
|
||||||
max_length=9, choices=country_name_choices, default="Canada",
|
max_length=9, choices=country_name_choices, default="Canada",
|
||||||
)
|
)
|
||||||
taxed: ormar.Boolean(choices=country_taxed_choices, default=True)
|
taxed= ormar.Boolean(choices=country_taxed_choices, default=True)
|
||||||
country_code: ormar.Integer(
|
country_code = ormar.Integer(
|
||||||
minimum=0, maximum=1000, choices=country_country_code_choices, default=1
|
minimum=0, maximum=1000, choices=country_country_code_choices, default=1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pytest
|
import pytest
|
||||||
@ -35,8 +35,8 @@ class Category(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Item(ormar.Model):
|
class Item(ormar.Model):
|
||||||
@ -45,9 +45,9 @@ class Item(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category = ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pytest
|
import pytest
|
||||||
@ -17,8 +18,8 @@ class Department(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True, autoincrement=False)
|
id = ormar.Integer(primary_key=True, autoincrement=False)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class SchoolClass(ormar.Model):
|
class SchoolClass(ormar.Model):
|
||||||
@ -27,8 +28,8 @@ class SchoolClass(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Category(ormar.Model):
|
class Category(ormar.Model):
|
||||||
@ -37,9 +38,9 @@ class Category(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
department: ormar.ForeignKey(Department, nullable=False)
|
department= ormar.ForeignKey(Department, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
class Student(ormar.Model):
|
class Student(ormar.Model):
|
||||||
@ -48,10 +49,10 @@ class Student(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
schoolclass: ormar.ForeignKey(SchoolClass)
|
schoolclass= ormar.ForeignKey(SchoolClass)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category= ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class Teacher(ormar.Model):
|
class Teacher(ormar.Model):
|
||||||
@ -60,10 +61,10 @@ class Teacher(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
schoolclass: ormar.ForeignKey(SchoolClass)
|
schoolclass= ormar.ForeignKey(SchoolClass)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category= ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
|
|||||||
@ -18,8 +18,8 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: str = ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Track(ormar.Model):
|
class Track(ormar.Model):
|
||||||
@ -28,10 +28,10 @@ class Track(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
album: Optional[Album] = ormar.ForeignKey(Album)
|
album= ormar.ForeignKey(Album)
|
||||||
title: str = ormar.String(max_length=100)
|
title = ormar.String(max_length=100)
|
||||||
position: int = ormar.Integer()
|
position = ormar.Integer()
|
||||||
|
|
||||||
|
|
||||||
class Cover(ormar.Model):
|
class Cover(ormar.Model):
|
||||||
@ -40,9 +40,9 @@ class Cover(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
album: Album = ormar.ForeignKey(Album, related_name="cover_pictures")
|
album= ormar.ForeignKey(Album, related_name="cover_pictures")
|
||||||
title: str = ormar.String(max_length=100)
|
title = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Organisation(ormar.Model):
|
class Organisation(ormar.Model):
|
||||||
@ -51,8 +51,8 @@ class Organisation(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
|
ident = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
|
||||||
|
|
||||||
|
|
||||||
class Team(ormar.Model):
|
class Team(ormar.Model):
|
||||||
@ -61,9 +61,9 @@ class Team(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
org: Optional[Organisation] = ormar.ForeignKey(Organisation)
|
org= ormar.ForeignKey(Organisation)
|
||||||
name: str = ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Member(ormar.Model):
|
class Member(ormar.Model):
|
||||||
@ -72,9 +72,9 @@ class Member(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
team: Optional[Team] = ormar.ForeignKey(Team)
|
team= ormar.ForeignKey(Team)
|
||||||
email: str = ormar.String(max_length=100)
|
email = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -21,8 +21,8 @@ class Model(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.String(primary_key=True, default=key, max_length=8)
|
id = ormar.String(primary_key=True, default=key, max_length=8)
|
||||||
name: ormar.String(max_length=32)
|
name = ormar.String(max_length=32)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="function")
|
@pytest.fixture(autouse=True, scope="function")
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
@ -16,10 +18,10 @@ class Book(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
title: ormar.String(max_length=200)
|
title = ormar.String(max_length=200)
|
||||||
author: ormar.String(max_length=100)
|
author = ormar.String(max_length=100)
|
||||||
genre: ormar.String(
|
genre = ormar.String(
|
||||||
max_length=100,
|
max_length=100,
|
||||||
default="Fiction",
|
default="Fiction",
|
||||||
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
|
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
|
||||||
@ -32,9 +34,9 @@ class ToDo(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
text: ormar.String(max_length=500)
|
text = ormar.String(max_length=500)
|
||||||
completed: ormar.Boolean(default=False)
|
completed= ormar.Boolean(default=False)
|
||||||
|
|
||||||
|
|
||||||
class Category(ormar.Model):
|
class Category(ormar.Model):
|
||||||
@ -43,8 +45,8 @@ class Category(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=500)
|
name = ormar.String(max_length=500)
|
||||||
|
|
||||||
|
|
||||||
class Note(ormar.Model):
|
class Note(ormar.Model):
|
||||||
@ -53,9 +55,9 @@ class Note(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
text: ormar.String(max_length=500)
|
text = ormar.String(max_length=500)
|
||||||
category: ormar.ForeignKey(Category)
|
category= ormar.ForeignKey(Category)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pytest
|
import pytest
|
||||||
@ -17,8 +18,8 @@ class Department(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True, autoincrement=False)
|
id = ormar.Integer(primary_key=True, autoincrement=False)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class SchoolClass(ormar.Model):
|
class SchoolClass(ormar.Model):
|
||||||
@ -27,9 +28,9 @@ class SchoolClass(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
department: ormar.ForeignKey(Department, nullable=False)
|
department= ormar.ForeignKey(Department, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
class Category(ormar.Model):
|
class Category(ormar.Model):
|
||||||
@ -38,8 +39,8 @@ class Category(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class Student(ormar.Model):
|
class Student(ormar.Model):
|
||||||
@ -48,10 +49,10 @@ class Student(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
schoolclass: ormar.ForeignKey(SchoolClass)
|
schoolclass= ormar.ForeignKey(SchoolClass)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category= ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class Teacher(ormar.Model):
|
class Teacher(ormar.Model):
|
||||||
@ -60,10 +61,10 @@ class Teacher(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
schoolclass: ormar.ForeignKey(SchoolClass)
|
schoolclass= ormar.ForeignKey(SchoolClass)
|
||||||
category: ormar.ForeignKey(Category, nullable=True)
|
category= ormar.ForeignKey(Category, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import databases
|
import databases
|
||||||
import pydantic
|
import pydantic
|
||||||
import pytest
|
import pytest
|
||||||
@ -16,9 +18,9 @@ class Company(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100, nullable=False)
|
name = ormar.String(max_length=100, nullable=False)
|
||||||
founded: ormar.Integer(nullable=True)
|
founded = ormar.Integer(nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class Car(ormar.Model):
|
class Car(ormar.Model):
|
||||||
@ -27,13 +29,13 @@ class Car(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
manufacturer: ormar.ForeignKey(Company)
|
manufacturer= ormar.ForeignKey(Company)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
year: ormar.Integer(nullable=True)
|
year = ormar.Integer(nullable=True)
|
||||||
gearbox_type: ormar.String(max_length=20, nullable=True)
|
gearbox_type = ormar.String(max_length=20, nullable=True)
|
||||||
gears: ormar.Integer(nullable=True)
|
gears = ormar.Integer(nullable=True)
|
||||||
aircon_type: ormar.String(max_length=20, nullable=True)
|
aircon_type = ormar.String(max_length=20, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
|||||||
@ -20,11 +20,11 @@ class Product(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
company: ormar.String(max_length=200, server_default="Acme")
|
company = ormar.String(max_length=200, server_default="Acme")
|
||||||
sort_order: ormar.Integer(server_default=text("10"))
|
sort_order = ormar.Integer(server_default=text("10"))
|
||||||
created: ormar.DateTime(server_default=func.now())
|
created= ormar.DateTime(server_default=func.now())
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
|
|||||||
@ -21,9 +21,9 @@ class Product(ormar.Model):
|
|||||||
database = database
|
database = database
|
||||||
constraints = [ormar.UniqueColumns("name", "company")]
|
constraints = [ormar.UniqueColumns("name", "company")]
|
||||||
|
|
||||||
id: ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name: ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
company: ormar.String(max_length=200)
|
company = ormar.String(max_length=200)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
|
|||||||
Reference in New Issue
Block a user