Skip to content

Instantly share code, notes, and snippets.

@rfinz
Last active December 20, 2015 12:09
Show Gist options
  • Save rfinz/6128686 to your computer and use it in GitHub Desktop.
Save rfinz/6128686 to your computer and use it in GitHub Desktop.
Snippet that initializes a DB with all the tables defined in a peewee ORM model... in this case "MariaModel". Add tables to the model at any point and the program should be able to set up the DB programmatically.
for t in [ name for name,c in \
inspect.getmembers(sys.modules[__name__],inspect.isclass) \
if issubclass(c, MariaModel) and not c is MariaModel]:
try:
globals()[t].create_table()
except Exception as e:
print e
@rfinz
Copy link
Author

rfinz commented Aug 12, 2013

Only downside is that it doesn't ensure hierarchical creation of the tables and therefore foreign key enforcement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment