Reflection
The reflection module contains helpers for introspecting existing databases. This module is used internally by several other modules in the playhouse, including DataSet and pwiz, a model generator.
generate_models
(database[, schema=None[, \*options*]])
Parameters: |
|
---|---|
Returns: | a |
Generate models for the tables in the given database. For an example of how to use this function, see the section Using Peewee Interactively.
Example:
>>> from peewee import *
>>> from playhouse.reflection import generate_models
>>> db = PostgresqlDatabase('my_app')
>>> models = generate_models(db)
>>> list(models.keys())
['account', 'customer', 'order', 'orderitem', 'product']
>>> globals().update(models) # Inject models into namespace.
>>> for cust in customer.select(): # Query using generated model.
... print(cust.name)
...
Huey Kitty
Mickey Dog
print_model
(model)
Parameters: | model (Model) – model class to print |
---|---|
Returns: | no return value |
Print a user-friendly description of a model class, useful for debugging or interactive use. Currently this prints the table name, and all fields along with their data-types. The Using Peewee Interactively section contains an example.
Example output:
>>> from playhouse.reflection import print_model
>>> print_model(User)
user
id AUTO PK
email TEXT
name TEXT
dob DATE
index(es)
email UNIQUE
>>> print_model(Tweet)
tweet
id AUTO PK
user INT FK: User.id
title TEXT
content TEXT
timestamp DATETIME
is_published BOOL
index(es)
user_id
is_published, timestamp
print_table_sql
(model)
Parameters: | model (Model) – model to print |
---|---|
Returns: | no return value |
Prints the SQL CREATE TABLE
for the given model class, which may be useful for debugging or interactive use. See the Using Peewee Interactively section for example usage. Note that indexes and constraints are not included in the output of this function.
Example output:
>>> from playhouse.reflection import print_table_sql
>>> print_table_sql(User)
CREATE TABLE IF NOT EXISTS "user" (
"id" INTEGER NOT NULL PRIMARY KEY,
"email" TEXT NOT NULL,
"name" TEXT NOT NULL,
"dob" DATE NOT NULL
)
>>> print_table_sql(Tweet)
CREATE TABLE IF NOT EXISTS "tweet" (
"id" INTEGER NOT NULL PRIMARY KEY,
"user_id" INTEGER NOT NULL,
"title" TEXT NOT NULL,
"content" TEXT NOT NULL,
"timestamp" DATETIME NOT NULL,
"is_published" INTEGER NOT NULL,
FOREIGN KEY ("user_id") REFERENCES "user" ("id")
)
class Introspector
(metadata[, schema=None])
Metadata can be extracted from a database by instantiating an Introspector
. Rather than instantiating this class directly, it is recommended to use the factory method from_database()
.
classmethod
from_database
(database[, schema=None])Parameters: - database – a
Database
instance. - schema (str) – an optional schema (supported by some databases).
Creates an
Introspector
instance suitable for use with the given database.Usage:
db = SqliteDatabase('my_app.db')
introspector = Introspector.from_database(db)
models = introspector.generate_models()
# User and Tweet (assumed to exist in the database) are
# peewee Model classes generated from the database schema.
User = models['user']
Tweet = models['tweet']
- database – a
generate_models
([skip_invalid=False[, table_names=None[, literal_column_names=False[, bare_fields=False[, include_views=False]]]]])Parameters: - skip_invalid (bool) – Skip tables whose names are invalid python identifiers.
- table_names (list) – List of table names to generate. If unspecified, models are generated for all tables.
- literal_column_names (bool) – Use column-names as-is. By default, column names are “python-ized”, i.e. mixed-case becomes lower-case.
- bare_fields – SQLite-only. Do not specify data-types for introspected columns.
- include_views – generate models for VIEWs as well.
Returns: A dictionary mapping table-names to model classes.
Introspect the database, reading in the tables, columns, and foreign key constraints, then generate a dictionary mapping each database table to a dynamically-generated
Model
class.