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 dict mapping table names to model classes.

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:

  1. >>> from peewee import *
  2. >>> from playhouse.reflection import generate_models
  3. >>> db = PostgresqlDatabase('my_app')
  4. >>> models = generate_models(db)
  5. >>> list(models.keys())
  6. ['account', 'customer', 'order', 'orderitem', 'product']
  7. >>> globals().update(models) # Inject models into namespace.
  8. >>> for cust in customer.select(): # Query using generated model.
  9. ... print(cust.name)
  10. ...
  11. Huey Kitty
  12. 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:

  1. >>> from playhouse.reflection import print_model
  2. >>> print_model(User)
  3. user
  4. id AUTO PK
  5. email TEXT
  6. name TEXT
  7. dob DATE
  8. index(es)
  9. email UNIQUE
  10. >>> print_model(Tweet)
  11. tweet
  12. id AUTO PK
  13. user INT FK: User.id
  14. title TEXT
  15. content TEXT
  16. timestamp DATETIME
  17. is_published BOOL
  18. index(es)
  19. user_id
  20. 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:

  1. >>> from playhouse.reflection import print_table_sql
  2. >>> print_table_sql(User)
  3. CREATE TABLE IF NOT EXISTS "user" (
  4. "id" INTEGER NOT NULL PRIMARY KEY,
  5. "email" TEXT NOT NULL,
  6. "name" TEXT NOT NULL,
  7. "dob" DATE NOT NULL
  8. )
  9. >>> print_table_sql(Tweet)
  10. CREATE TABLE IF NOT EXISTS "tweet" (
  11. "id" INTEGER NOT NULL PRIMARY KEY,
  12. "user_id" INTEGER NOT NULL,
  13. "title" TEXT NOT NULL,
  14. "content" TEXT NOT NULL,
  15. "timestamp" DATETIME NOT NULL,
  16. "is_published" INTEGER NOT NULL,
  17. FOREIGN KEY ("user_id") REFERENCES "user" ("id")
  18. )

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:

    1. db = SqliteDatabase('my_app.db')
    2. introspector = Introspector.from_database(db)
    3. models = introspector.generate_models()
    4. # User and Tweet (assumed to exist in the database) are
    5. # peewee Model classes generated from the database schema.
    6. User = models['user']
    7. Tweet = models['tweet']
  • 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_fieldsSQLite-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.