#๐Ÿ”’ someone help pls sqlalchemy RuntimeError

161 messages ยท Page 1 of 1 (latest)

lethal oriole
#
from flaskblog import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)```
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flaskblog.config import Config


db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bcrypt.init_app(app)
    login_manager.init_app(app)
    mail.init_app(app)

    from flaskblog.users.routes import users
    from flaskblog.posts.routes import posts
    from flaskblog.main.routes import main
    app.register_blueprint(users)
    app.register_blueprint(posts)
    app.register_blueprint(main)

    return app```
agile reefBOT
#

@lethal oriole

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lethal oriole
#

I wrote the code above, as well.

#

What do I need to do to fix this...?

verbal gyro
#

set the URI app.config['...'] = "mysql...

lethal oriole
#

Where do I do that?

#

init? run?

lethal oriole
lethal oriole
#

I'm not sure where I need to put that...

#

@verbal gyro

#

Someone help...I really want to make this code work...

#

So...I tried putting app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

#

But...

#

Now it gave another error.

#

Can someone tell me what I need to do?

#

app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///tmp/test.db' I tried it with this, 3 /'s instead of 4, and...

#

it still doesn't work...

#

Hmmmm...

#

I'm trying some other stuff, but it still doesn't seem to work...

#

I'm searching it on youtube rn.

#

No proper result...

#

Now it gives this error...

#

I've tried so many diffferent things, and none of them work...

#

@verbal gyro can you help?

#

someone help pls

#

someone help pls sqlalchemy error

#

This is the last thing I'll do with Flask for now, so I'd REALLY appreciate help.

#

Everything has worked thus far, but the LAST thing doesn't work...

#

๐Ÿ˜”

#

I just need one person to help, man...

#

someone help pls sqlalchemy RuntimeError

empty pendant
#

๐Ÿ—ฟ

#

hello

lethal oriole
#

Hello

unborn abyss
#

Hey @lethal oriole

#

Yesterday I saw your post was closed by you so I assumed you found a sol

unborn abyss
#
from app import app
from app.models import (Applications, ApplicationStatus, Courses, Deans,
                        Professors, Students, db)

print("Database URI:", app.config["SQLALCHEMY_DATABASE_URI"])

with app.app_context():
    db.create_all()
    print("Database created")

Write a script like this and run it. This will create the db first

#

fix the import according to your application

unborn abyss
lethal oriole
#

Nobody answered so

#

I had to close it.

#

I'll open my pc one sec.

lethal oriole
lethal oriole
#

Let me send the imports.

#
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flaskblog.config import Config```
#

These?

lethal oriole
#

Command prompt or?

lethal oriole
#

"from app" gives a red underline...?

lethal oriole
#

Yeah

#

No matter

#

where I put the app

#

it gives a red underline.

#

@unborn abyss

#

It says no module named app in the command prompt.

#

and in python it's red.

unborn abyss
#

alright

#

ok

#

so

#

you have a project src dir right?

lethal oriole
#

I do remember src being somewhere, yeah.

unborn abyss
#

Create this file as 'temp_create_db.py' or something

#

or

#

ykw

#

forget the filw

lethal oriole
#

This is how it looks like.

unborn abyss
#

beautiful, I was about to ask for the dir structure haha

lethal oriole
#

Okay

#

What do I do now?

unborn abyss
#

you already have your db in instance dir

#

did you create them?

lethal oriole
#

yeah

unborn abyss
#

alright then

lethal oriole
#
from flaskblog import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)```
unborn abyss
#

hmm

#

so the app object is created in flaskblog file

lethal oriole
#

yes.

unborn abyss
#

can you share me the code in it?

lethal oriole
#

Which code?

unborn abyss
#

can you share the code where the app obj is created?

#

ig it should be somewhere in main dir

lethal oriole
#

hmmmm

unborn abyss
#

or init file, im not sure

lethal oriole
#

It doesn't seem to be there...

#

main init is empty.

#

Let me check the snippets.

unborn abyss
lethal oriole
#

yeah main init is empty.

lethal oriole
unborn abyss
#

have you pushed all the code to a repo?

lethal oriole
#

repo?

#

All the code

lethal oriole
#

These files.

unborn abyss
#

sqlite:///instance/data.db try this as the uri value

lethal oriole
#

hmmm.

#

in config, right?

#

or wait

#
import os


class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
    MAIL_SERVER = 'smtp.googlemail.com'
    MAIL_PORT = 587
    MAIL_USE_TLS = True
    MAIL_USERNAME = os.environ.get('EMAIL_USER')
    MAIL_PASSWORD = os.environ.get('EMAIL_PASS')```
#

Do I write that here, then?

#

since flaskblog init doesn't have it

unborn abyss
#

yes

lethal oriole
#

instead of the database_url?

unborn abyss
#

no

#

SQLALCHEMY_DATABASE_URI = "..."

lethal oriole
#

Oh

#

okay let me try, hold on.

unborn abyss
#

however later you will need to fetch the value from env vars

lethal oriole
#

it's red...

#
import os


class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_DATABASE_URI = sqlite:///instance/data.db 
    MAIL_SERVER = 'smtp.googlemail.com'
    MAIL_PORT = 587
    MAIL_USE_TLS = True
    MAIL_USERNAME = os.environ.get('EMAIL_USER')
    MAIL_PASSWORD = os.environ.get('EMAIL_PASS')```
#

oh wait

#

I added '

unborn abyss
#

as a string dude

lethal oriole
#

yes yes

#

I realized that lol.

#

Hmmmm, I ran it...but will it work...?

#

...it didn't.

#

This same error again...

lethal oriole
lethal oriole
#

Why can't it just work???

unborn abyss
#

lets try

#

sqlite:////instance/data.db

#

this value

lethal oriole
#

4 /'s?

unborn abyss
#

yeah

#

not sure

lethal oriole
#

I tried that before, but it didn't work.

unborn abyss
#

try it now

lethal oriole
#

Okay let me see.

#

Yep.

#

Same error.

unborn abyss
#

which file contains this function: create_app

lethal oriole
#

let me check.

lethal oriole
#

I wonder if it's just as you said, it's just old...?

#

The code did work in the video, but that was in 2018...

lethal oriole
# unborn abyss which file contains this function: create_app
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flaskblog.config import Config
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bcrypt.init_app(app)
    login_manager.init_app(app)
    mail.init_app(app)

    from flaskblog.users.routes import users
    from flaskblog.posts.routes import posts
    from flaskblog.main.routes import main
    app.register_blueprint(users)
    app.register_blueprint(posts)
    app.register_blueprint(main)

    return app```
#

This also has the create_app.

#

@unborn abyss

lethal oriole
lethal oriole
#

I guess I will never know the answer to this error... ๐Ÿ’€

agile reefBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.