pwiz, a model generator

pwiz is a little script that ships with peewee and is capable ofintrospecting an existing database and generating model code suitable forinteracting with the underlying data. If you have a database already, pwiz cangive you a nice boost by generating skeleton code with correct columnaffinities and foreign keys.

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

  1. python -m pwiz -e postgresql -u postgres my_postgres_db

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
  1. >>> from mymodels import Blog, Entry, Tag, Whatever
  2. >>> print [blog.name for blog in Blog.select()]

Command-line options

pwiz accepts the following command-line options:

OptionMeaningExample
-hshow help
-edatabase backend-e mysql
-Hhost to connect to-H remote.db.server
-pport to connect on-p 9001
-udatabase user-u postgres
-Pdatabase password-P (will be prompted for password)
-sschema-s public
-ttables to generate-t tweet,users,relationships
-vgenerate models for VIEWs(no argument)
-iadd info metadata to generated file(no argument)
-otable column order is preserved(no argument)

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

  • sqlite
  • mysql
  • postgresql

Warning

If a password is required to access your database, you will be prompted toenter it using a secure prompt.

The password will be included in the output. Specifically, at the topof the file a Database will be defined along with any requiredparameters – including the password.

pwiz examples

Examples of introspecting various databases:

  1. # Introspect a Sqlite database.
  2. python -m pwiz -e sqlite path/to/sqlite_database.db
  3.  
  4. # Introspect a MySQL database, logging in as root. You will be prompted
  5. # for a password ("-P").
  6. python -m pwiz -e mysql -u root -P mysql_db_name
  7.  
  8. # Introspect a Postgresql database on a remote server.
  9. python -m pwiz -e postgres -u postgres -H 10.1.0.3 pg_db_name

Full example:

  1. $ sqlite3 example.db << EOM
  2. CREATE TABLE "user" ("id" INTEGER NOT NULL PRIMARY KEY, "username" TEXT NOT NULL);
  3. CREATE TABLE "tweet" (
  4. "id" INTEGER NOT NULL PRIMARY KEY,
  5. "content" TEXT NOT NULL,
  6. "timestamp" DATETIME NOT NULL,
  7. "user_id" INTEGER NOT NULL,
  8. FOREIGN KEY ("user_id") REFERENCES "user" ("id"));
  9. CREATE UNIQUE INDEX "user_username" ON "user" ("username");
  10. EOM
  11.  
  12. $ python -m pwiz -e sqlite example.db

Produces the following output:

  1. from peewee import *
  2.  
  3. database = SqliteDatabase('example.db', **{})
  4.  
  5. class UnknownField(object):
  6. def __init__(self, *_, **__): pass
  7.  
  8. class BaseModel(Model):
  9. class Meta:
  10. database = database
  11.  
  12. class User(BaseModel):
  13. username = TextField(unique=True)
  14.  
  15. class Meta:
  16. table_name = 'user'
  17.  
  18. class Tweet(BaseModel):
  19. content = TextField()
  20. timestamp = DateTimeField()
  21. user = ForeignKeyField(column_name='user_id', field='id', model=User)
  22.  
  23. class Meta:
  24. table_name = 'tweet'

Observations:

  • The foreign-key Tweet.user_id is detected and mapped correctly.
  • The User.username UNIQUE constraint is detected.
  • Each model explicitly declares its table name, even in cases where it is notnecessary (as Peewee would automatically translate the class name into theappropriate table name).
  • All the parameters of the ForeignKeyField are explicitlydeclared, even though they follow the conventions Peewee uses by default.

Note

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