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.

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]]]])

    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.
    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.