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.
Parameters:
- database (Database) – database instance to introspect.
- schema (str) – optional schema to introspect.
- options – arbitrary options, see
Introspector.generate_models()
for details.Returns:adict
mapping table names to model classes.
Generate models for the tables in the given database. For an example of howto 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
Parameters:model (Model) – model class to printReturns:no return value
Print a user-friendly description of a model class, useful for debugging orinteractive use. Currently this prints the table name, and all fields alongwith 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
Parameters:model (Model) – model to printReturns:no return value
Prints the SQL CREATE TABLE
for the given model class, which may beuseful for debugging or interactive use. See the Using Peewee Interactively sectionfor example usage. Note that indexes and constraints are not included inthe 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, itis recommended to use the factory methodfrom_database()
.
Parameters:
- **database** – a [<code>Database</code>]($91d5d4e449d7d4b4.md#Database) instance.
- **schema** (_str_) – an optional schema (supported by some databases).
Creates an Introspector
instance suitable for use with thegiven 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']
generatemodels
([_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 pythonidentifiers.
- **table_names** (_list_) – List of table names to generate. Ifunspecified, 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 forintrospected 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 foreignkey constraints, then generate a dictionary mapping each database tableto a dynamically-generated Model
class.