Install and First Query¶
This guide gets you from zero to a running model query as quickly as possible.
1. Install Edgy¶
For SQLite:
$ pip install edgy[sqlite]
For Postgres:
$ pip install edgy[postgres]
2. Define Your First Model¶
The following example is enough to create a model, create a record, and read it back.
Use ipython so you can run await directly.
import edgy
from edgy import Database, Registry
database = Database("sqlite:///db.sqlite")
models = Registry(database=database)
class User(edgy.Model):
"""
The User model to be created in the database as a table
If no name is provided the in Meta class, it will generate
a "users" table for you.
"""
is_active: bool = edgy.BooleanField(default=False)
class Meta:
registry = models
# Create the db and tables
# Don't use this in production! Use Alembic or any tool to manage
# The migrations for you
await models.create_all() # noqa
await User.query.create(is_active=False) # noqa
user = await User.query.get(id=1) # noqa
print(user)
# User(id=1)
3. Extend the Query¶
Now that your model works, try chaining a filter:
users = await User.query.filter(is_active=False).order_by("id").all()
Then inspect only selected fields:
user_rows = await User.query.only("id").values()
4. Understand What Happened¶
At a high level:
- you declared a model bound to a registry,
- Edgy translated model metadata to SQL,
- QuerySet compiled and executed a query,
- result rows were parsed back into model instances.
For the deeper runtime flow, read Request and Query Lifecycle.
5. Next Step¶
Continue with First Migration Cycle to make schema changes reproducible.