Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python [Flask] I have error with my database (flask shell runtime error)

Kaworu

Active Coder
Hi!

I am still trying to learn Flask. And I have some problems that I cannot fix myself.

Okay, so now I have a code (classes) that define a database. And I would like to generate the database in flash shell. However, I have some fundamental error with my database, yet I do not know why.

My code:

Python:
from flask import Flask, render_template, session, redirect, url_for, flash

from flask_bootstrap import Bootstrap

from flask_moment import Moment

from datetime import datetime

from flask_wtf import FlaskForm

from wtforms import StringField, SubmitField

from wtforms.validators import DataRequired

import os

from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)

bootstrap = Bootstrap(app)

moment = Moment(app)

db = SQLAlchemy(app)

app.config["SQLALCHEMY_DATABASE_URL"] = "sqlite:///" + os.path.join(basedir, "my-database.db")

app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

app.config["SECRET_KEY"] = "guess me!!!"

db.init_app(app)

# then go classes, routes etc – I believe everything there is fine, so I am not spamming you

And the error I get when I run flask shell in cmd:

Code:
Traceback (most recent call last):

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main

return _run_code(code, main_globals, None,

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code

exec(code, run_globals)

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\Scripts\flask.exe\__main__.py", line 7, in <module>

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 1064, in main

cli.main()

File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1055, in main

rv = self.invoke(ctx)

File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1657, in invoke

return _process_result(sub_ctx.command.invoke(sub_ctx))

File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 1404, in invoke

return ctx.invoke(self.callback, **ctx.params)

File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\click\core.py", line 760, in invoke

return __callback(*args, **kwargs)

File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\click\decorators.py", line 26, in new_func

return f(get_current_context(), *args, **kwargs)

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 355, in decorator

app = __ctx.ensure_object(ScriptInfo).load_app()

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 313, in load_app

app = locate_app(import_name, None, raise_if_not_found=False)

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 219, in locate_app

__import__(module_name)

File "E:\Personal Data\My Folders\Programowanie\Python3\Flasky\My Code\5\SQLAlchemy\app.py", line 21, in <module>

db = SQLAlchemy(app)

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_sqlalchemy\extension.py", line 278, in __init__

self.init_app(app)

File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_sqlalchemy\extension.py", line 355, in init_app

raise RuntimeError(

RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.

I feel very lost (also, it’s my first time ever doing something like this). Any help would be greatly appreciated ;-)
 
Well, there are some configuration error with your code, can you try below code, I hope you can fix the error you are getting.

Code:
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from datetime import datetime
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
import os
from flask_sqlalchemy import SQLAlchemy


basedir = os.path.abspath(os.path.dirname(__file__))


app = Flask(__name__)


bootstrap = Bootstrap(app)
moment = Moment(app)


# Correct the configuration name to 'SQLALCHEMY_DATABASE_URI'
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "my-database.db")


app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = "guess me!!!"


db = SQLAlchemy(app)


# The rest of your code for defining classes, routes, etc.


Thanks
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom