Skip to content

Commit

Permalink
Additional docs and small fix to print_model().
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Mar 6, 2019
1 parent 1f86371 commit 57e7910
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
74 changes: 73 additions & 1 deletion docs/peewee/playhouse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,25 @@ including :ref:`dataset` and :ref:`pwiz`.
Generate models for the tables in the given database. For an example of how
to use this function, see the section :ref:`interactive`.

Example:

.. code-block:: pycon
>>> 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
.. py:function:: print_model(model)
:param Model model: model class to print
Expand All @@ -3081,14 +3100,67 @@ including :ref:`dataset` and :ref:`pwiz`.
interactive use. Currently this prints the table name, and all fields along
with their data-types. The :ref:`interactive` section contains an example.

Example output:

.. code-block:: pycon
>>> 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
.. py:function:: print_table_sql(model)
:param 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 :ref:`interactive` section
for example usage.
for example usage. Note that indexes and constraints are not included in
the output of this function.

Example output:

.. code-block:: pycon
>>> 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")
)
.. py:class:: Introspector(metadata[, schema=None])
Expand Down
8 changes: 6 additions & 2 deletions playhouse/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,13 @@ def print_model(model, indexes=True, inline_indexes=False):
field.rel_field.name))
print(''.join(parts))

if indexes and model._meta.indexes:
if indexes:
index_list = model._meta.fields_to_index()
if not index_list:
return

print('\nindex(es)')
for index in model._meta.fields_to_index():
for index in index_list:
parts = [' ']
ctx = model._meta.database.get_sql_context()
with ctx.scope_values(param='%s', quote='""'):
Expand Down

0 comments on commit 57e7910

Please sign in to comment.