Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models Manager #31

Closed
ghost opened this issue Oct 10, 2021 · 0 comments
Closed

Models Manager #31

ghost opened this issue Oct 10, 2021 · 0 comments
Labels
enhancement New feature or request hacktoberfest

Comments

@ghost
Copy link

ghost commented Oct 10, 2021

Feature description::

Create a models manager, this manager will have all the models and it will be here that it will contain several ready-made functions. As the create_tables method that will create tables regardless of having to create one table before another, it will do this automatically. It will also make it easier to create future features.

Need:

from databases import Database
from duck_orm.sql import fields as Field
from duck_orm.model import Model, ManagerModel

db = Database('sqlite:///example.db')
manager = ManagerModel()
await db.connect()

class City(Model):
    __tablename__ = 'cities'
    __db__ = db
    manager = manager

    id: int = Field.Integer(primary_key=True, auto_increment=True)
    name: str = Field.String(unique=True)

    def relationships(self):
        self.persons = OneToMany(
            model=Person,
            name_in_table_fk='city',
            name_relation='person_city'
        )


class Person(Model):
    __tablename__ = 'persons'
    __db__ = db
    manager = manager

    id: 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()
    city: City = ForeignKey(model=City, name_in_table_fk='id')

# He will know that he must create the `City` table first.
await manager.create_tables()
@ghost ghost added enhancement New feature or request hacktoberfest labels Oct 10, 2021
@ghost ghost self-assigned this Oct 10, 2021
This was referenced Oct 17, 2021
@ghost ghost closed this as completed Oct 21, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest
Projects
None yet
Development

No branches or pull requests

0 participants