Probleme mit SQLAlchemy Models

Django, Flask, Bottle, WSGI, CGI…
Antworten
Benutzeravatar
Domroon
User
Beiträge: 104
Registriert: Dienstag 3. November 2020, 10:27
Wohnort: Dortmund

Hallo Leute,

ich habe Probleme mit SQLAlchemy in flask.
Ich habe mich durch die offizielle Doku durchgearbeitet. Leider wird in dem Tutorial in der Doku (https://flask.palletsprojects.com/en/2.0.x/tutorial/) direkt mit SQLite gearbeitet. Ich möchte stattdessen aber lieber mit SQLAlchemy arbeiten. Ich halte mich daran was ich aus der Doku gelernt habe aber irgendwo scheint ein dicker Gedankenfehler zu sein. Meinen Code findet ihr hier (ist zu viel um es zu posten):
https://github.com/Domroon/theurer_proj ... sk_website

Dieser Hinzugefügte Befehl funktioniert. Hierdurch wird eine SQLite Datenbank erstellt mit dem Model "User":

Code: Alles auswählen

flask init-db
Wenn ich aber

Code: Alles auswählen

flask run
ausführe dann bekomme ich folgende Fehlermeldung:

Code: Alles auswählen

(venv) C:\Users\haeus\workspace\theurer_projekt>flask run
 * Serving Flask app 'flask_website' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\haeus\workspace\theurer_projekt\venv\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 990, in main
    cli.main(args=sys.argv[1:])
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 596, in main
    return super().main(*args, **kwargs)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\click\core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\click\core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\click\core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\click\decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\click\core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 845, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 321, in __init__
    self._load_unlocked()
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 346, in _load_unlocked
    self._app = rv = self.loader()
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 402, in load_app
    app = locate_app(self, import_name, name)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 273, in locate_app
    return find_best_app(script_info, module)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 68, in find_best_app
    app = call_factory(script_info, app_factory)
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\cli.py", line 119, in call_factory
    return app_factory(*args, **kwargs)
  File "C:\Users\haeus\workspace\theurer_projekt\flask_website\__init__.py", line 37, in create_app
    from . import auth
  File "C:\Users\haeus\workspace\theurer_projekt\flask_website\auth.py", line 1, in <module>
    from flask_website.models import User
  File "C:\Users\haeus\workspace\theurer_projekt\flask_website\models.py", line 3, in <module>
    db = db_init.get_db()
  File "C:\Users\haeus\workspace\theurer_projekt\flask_website\db.py", line 7, in get_db
    if 'db' not in g:
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\werkzeug\local.py", line 422, in __get__
    obj = instance._get_current_object()
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\werkzeug\local.py", line 544, in _get_current_object
    return self.__local()  # type: ignore
  File "c:\users\haeus\workspace\theurer_projekt\venv\lib\site-packages\flask\globals.py", line 40, in _lookup_app_object
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.
Ich weiß: Laut Fehlermeldung soll ich in die Doku schauen. Ich habe nur keine Ahnung was ich hier falsch mache. Hat jemand eine Idee was ich an meinem Code anders machen könnte oder was ich hier völlig falsche verstehe?
Ich wäre euch super dankbar ;)
DasIch
User
Beiträge: 2718
Registriert: Montag 19. Mai 2008, 04:21
Wohnort: Berlin

Das Problem ist https://github.com/Domroon/theurer_proj ... dels.py#L3. Die Funktion die da aufgerufen wird nutzt `current_app` https://github.com/Domroon/theurer_proj ... e/db.py#L8 was nur funktioniert wenn man in einem application context ist.
Benutzeravatar
Domroon
User
Beiträge: 104
Registriert: Dienstag 3. November 2020, 10:27
Wohnort: Dortmund

Okay dann müsste ich in models.py doch folgendes machen:

Code: Alles auswählen

from flask import current_app
from current app import db as database
Werde es nachher mal austesten ;)
Antworten