M
M
maximkalga2017-03-02 00:19:49
ORM
maximkalga, 2017-03-02 00:19:49

How to initialize tables in the database (syncdb) in Flask + Peewee ORM?

Didn't work with flask and its stack. You need to run the finished project. Here are its dependencies:

Flask==0.10.1
Flask-Login==0.3.1
Flask-Testing==0.4.2
Flask-WTF==0.12
Jinja2==2.8
MarkupSafe==0.23
WTForms==2.0.2
Werkzeug==0.10.4
argparse==1.2.1
flask-peewee==0.6.6
funcsigs==0.4
itsdangerous==0.24
mock==1.3.0
nose==1.3.7
pbr==1.8.0
peewee==2.6.4
psycopg2==2.6.1
requests==2.7.0
six==1.9.0
wsgiref==0.1.2
wtf-peewee==0.2.3


installed everything in a virtual machine, started the server, everything is ok

runserver.py

from myproject import create_app
app = create_app()

if __name__ == '__main__':
    app.run(debug=True, port=5000, host='0.0.0.0')

It remains to create tables in the database. Here is a description of models with peewee

models.py

from peewee import Model, CharField, IntegerField, DateTimeField, DecimalField, TextField, datetime as peewee_datetime
from playhouse.pool import PooledPostgresqlExtDatabase

from .config import BaseConfig


db = PooledPostgresqlExtDatabase(**BaseConfig.DATABASE)
db.commit_select = True
db.autorollback = True


class BaseModel(Model):
    class Meta:
        database = db

    def save(self, **kwds):
        with db.transaction():
            Model.save(self, **kwds)


class Payment(BaseModel):
    class Meta:
        db_table = "payments"

    card_number = CharField()
    amount = DecimalField()
    ...
    ...
    ...


def init_db():
    try:
        db.connect()
        map(lambda l: db.drop_table(l, True), (Payment,))
        print "tables dropped"
        map(lambda l: db.create_table(l, True), (Payment,))
        print "tables created"
    except:
        db.rollback()
        raise


How to create tables? Is there something like "python manage.py migrate" similar to Django?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maximkalga, 2017-03-02
@maximkalga

It was necessary to import init_db () from models.py in the shell and run it. Migrations from peewee are not used here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question