Models
With DuckORM
it's easy to create your database models and get started right away.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 from databases import Database
from duck_orm.model import Model
from duck_orm.model_manager import ModelManager
from duck_orm.sql import fields as Field
model_manager = ModelManager ()
db = Database ( 'sqlite:///example.db' )
await db . connect ()
class Person ( Model ):
__tablename__ = 'persons'
__db__ = db
model_manager = model_manager
id_teste : int = Field . Integer ( primary_key = True , auto_increment = True )
first_name : str = Field . String ( unique = True )
last_name : str = Field . String ( not_null = True )
age : int = Field . BigInteger ()
salary : int = Field . BigInteger ()
So far you've only defined a template, but you haven't told DuckORM to create
a table in the database, but it's easy to do that, just add a line of code:
await model_manager . create_all_tables ()
Definition of fields
And then just define the table fields .
Basic Types
For each table created, it must necessarily have a field with the attribute
primary_key=True
.
And only one primary_key
column is allowed.
class Person ( Model ):
__tablename__ = 'persons'
__db__ = db
model_manager = model_manager
id_teste : int = Field . Integer ( primary_key = True , auto_increment = True )
first_name : str = Field . String ( unique = True )
last_name : str = Field . String ( not_null = True )
age : int = Field . BigInteger ()
salary : int = Field . BigInteger ()
Warning
You should not assign more than one primary_key
to more than one column
in the same table.
Dependencies
DuckORM
depends on databases
library to connect to the database.
Table name
Another important parameter is __tablename__
, which is used to set the name
of your Model in the database.
class Person ( Model ):
__tablename__ = 'persons'
__db__ = db
model_manager = model_manager
id_teste : int = Field . Integer ( primary_key = True , auto_increment = True )
first_name : str = Field . String ( unique = True )
last_name : str = Field . String ( not_null = True )
age : int = Field . BigInteger ()
salary : int = Field . BigInteger ()
Tip
If you don't pass the __tablename__
attribute, the table name will be
defined by the name of the Model
.
Example: In the case above, if the __tablename__
attribute was not passed, the name
of the table would be person
.
Databases
This parameter is __db__
, and it is the instance create, bith your database
URL string.
This instance needs to be passed to the Model
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from databases import Database
from duck_orm.model import Model
from duck_orm.sql import fields as Field
db = Database ( 'sqlite:///example.db' )
await db . connect ()
class Person ( Model ):
__tablename__ = 'persons'
__db__ = db
model_manager = model_manager
id_teste : int = Field . Integer ( primary_key = True , auto_increment = True )
first_name : str = Field . String ( unique = True )
last_name : str = Field . String ( not_null = True )
age : int = Field . BigInteger ()
salary : int = Field . BigInteger ()
Tip
You must create the databases
instance only once and then
use it for all models of your system, but nothing stops you from creating
multiple instances Another important parameter is tablename , which is used to set the name of your Model in the database. if you want to use multiple databases .
Model Manager:
It is the database model manager. It is he who has the methods to create all the
tables and their relationships.
class Person ( Model ):
__tablename__ = 'persons'
__db__ = db
model_manager = model_manager
id_teste : int = Field . Integer ( primary_key = True , auto_increment = True )
first_name : str = Field . String ( unique = True )
last_name : str = Field . String ( not_null = True )
age : int = Field . BigInteger ()
salary : int = Field . BigInteger ()