question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: FlaskDB incorrect implementation

See original GitHub issue
  1. current implementation of FlaskDB https://github.com/coleifer/peewee/blob/master/playhouse/flask_utils.py#L87 is incorrect: it stores database in self instead of the flask context

  2. that prevents FlaskDB to be used with flask factory pattern for tests with multiple flask app instances using different per-app-instance database configurations created inside single python process

  3. see: https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/#factories-extensions https://flask.palletsprojects.com/en/1.1.x/extensiondev/#the-extension-code

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Andrei-Pozolotincommented, Aug 7, 2019

even more simplified example:

import peewee
from flask import Flask, current_app
from playhouse.sqlite_ext import SqliteExtDatabase

class PeeweeDatawrap:

    def init_app(self, app:Flask) -> None:
        app.extensions['peewee_database'] = self.produce_database(app)
        app.before_request(self.perform_open)
        app.teardown_request(self.perform_close)

    def perform_open(self, *args, **kwargs) -> None:
        self.database.connect()

    def perform_close(self, *args, **kwargs) -> None:
        self.database.close()

    def produce_database(self, app:Flask) -> peewee.Database:
        datafile = app.config['DATABASE']
        return SqliteExtDatabase(datafile)

    @property
    def database(self) -> peewee.Database:
        return current_app.extensions['peewee_database']
0reactions
Andrei-Pozolotincommented, Aug 13, 2019
  1. well, we do not want to follow low standards, do we? 😃

  2. on another note, this pattern is also a major pain:

class Model:
   class Meta:
      database: ...
  1. perhaps switching peewee default database injection to flask-like current_app analogue would be less evil
Read more comments on GitHub >

github_iconTop Results From Across the Web

flask db init leads to "KeyError: 'migrate' - Stack Overflow
I tried it for hours know but found no solution so far. What am I doing wrong? Please help me. python · flask...
Read more >
Error on flask db upgrade · Issue #341 - GitHub
Hi! I'm trying to implement flask_migrate on my project. I use app factory pattern and followed the instructions from the blog.
Read more >
Fixing ALTER TABLE errors with Flask-Migrate and SQLite
SQLite's ALTER TABLE Implementation ... The problem occurs when you try to upgrade the database with this ... Run flask db downgrade ....
Read more >
Fixing ALTER TABLE errors with Flask-Migrate and SQLite
In this video you can learn how to workaround the common ALTER TABLE errors that appear when migrating a SQLite database with Flask-Migrate ......
Read more >
Flask-DB Helps You Migrate, Seed and Reset Your ... - YouTube
It adds a new flask db command. For migrations it uses Alembic under the hood.Hit the subscribe button to receive more videos like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found