0
votes

I tried peewee with flask for two days, but I failed till now. The code is as the follows:

import click
from flask import Flask
from flask.cli import FlaskGroup

from playhouse.flask_utils import FlaskDB

from models import *
from config import config

flask_db = FlaskDB()


def create_app(config_name):
    application = Flask(__name__)
    application.config.from_object(config[config_name])
    flask_db.init_app(application)

    flask_db.connect_db()
    flask_db.database.create_tables([User])
    flask_db.database.close()

    @application.route('/')
    def index():
        return "hello world!"

    return application


def create_cli_app(info):
    return create_app("develop")


@click.group(cls=FlaskGroup, create_app=create_cli_app)
def cli():
    pass


@cli.command()
def initdb():
    flask_db.connect_db()
    flask_db.database.create_tables([User])
    flask_db.database.close()


if __name__ == "__main__":
    cli()

When I run it with the CLI: python manage.py run, I got the following errors:

(venv) ➜ /Users/yw/Documents/web git:(master) ✗ p manage.py run Traceback (most recent call last): File "manage.py", line 46, in cli() File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py", line 716, in call return self.main(*args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py", line 345, in main return AppGroup.main(self, *args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py", line 696, in main rv = self.invoke(ctx) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py", line 1060, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py", line 889, in invoke return ctx.invoke(self.callback, **ctx.params) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py", line 534, in invoke return callback(*args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/decorators.py", line 64, in new_func return ctx.invoke(f, obj, *args[1:], **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/click/core.py", line 534, in invoke return callback(*args, **kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py", line 388, in run_command app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py", line 124, in init self._load_unlocked() File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py", line 148, in _load_unlocked self._app = rv = self.loader() File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/flask/cli.py", line 201, in load_app rv = self.create_app(self) File "manage.py", line 30, in create_cli_app return create_app("develop") File "manage.py", line 19, in create_app flask_db.database.create_tables([User]) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/peewee.py", line 3765, in create_tables create_model_tables(models, fail_silently=safe) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/peewee.py", line 5175, in create_model_tables m.create_table(**create_table_kwargs) File "/Users/yw/Documents/web/venv/lib/python3.4/site-packages/peewee.py", line 4845, in create_table if db.sequences and pk is not False and pk.sequence: AttributeError: 'FlaskDB' object has no attribute 'sequences'

Indeed, I just want to initiate the DB by using flask.cli tool. As you see, if I use the command “python manage.py initdb”, I can only get the same error output as above.

So what is the meaning of "'FlaskDB' object has no attribute ‘sequences'"? What should I do?

Thanks for your help!

1
I solved the problem now, please close this post. I made a low level mistake. The problem happened in another file - models.py. In the class definition for those db tables, I should use flask_db.Model instead of Model to be the base class. Just this, now everything is OK. - urbainy
Stackoverflow is not strictly a "get free answers to your problems" site. It's a publicly post your problem and have a publicly documented solution. So post the solution as an answer, then it will be "closed". - Tomáš Pospíšek
Also, please edit the stacktrace so that its readable (it's missing newlines) - Tomáš Pospíšek

1 Answers

1
votes

It seems that your User model doesn't have correct database.

With FlaskDB, your User model should inherit FlaskDB().Model instead of defining class Meta: database = database.

database = FlaskDB()

class User(database.Model):
    pass