If you install peewee using setup.py install, pwiz will be installed as a “script” and you can just run:

    This will print a bunch of models to standard output. So you can do this:

    1. python -m pwiz -e postgresql my_postgres_db > mymodels.py
    2. python # <-- fire up an interactive shell

    The following are valid parameters for the engine (-e):

    • sqlite
    • mysql
    • postgresql

    pwiz examples

    Examples of introspecting various databases:

    1. # Introspect a Sqlite database.
    2. python -m pwiz -e sqlite path/to/sqlite_database.db
    3. # Introspect a MySQL database, logging in as root:secret.
    4. python -m pwiz -e mysql -u root -P secret mysql_db_name
    5. python -m pwiz -e postgres -u postgres -H 10.1.0.3 pg_db_name

    Produces the following output:

    1. database = SqliteDatabase('example.db', **{})
    2. class UnknownField(object):
    3. def __init__(self, *_, **__): pass
    4. class BaseModel(Model):
    5. class Meta:
    6. database = database
    7. class User(BaseModel):
    8. table_name = 'user'
    9. class Tweet(BaseModel):
    10. content = TextField()
    11. timestamp = DateTimeField()
    12. user = ForeignKeyField(column_name='user_id', field='id', model=User)
    13. class Meta:
    14. table_name = 'tweet'

    Observations:

    • The foreign-key Tweet.user_id is detected and mapped correctly.
    • The User.username UNIQUE constraint is detected.
    • All the parameters of the ForeignKeyField are explicitly declared, even though they follow the conventions Peewee uses by default.

    The UnknownField is a placeholder that is used in the event your schema contains a column declaration that Peewee doesn’t know how to map to a field class.