Python Flask Tutorial: Full-Featured Web App Part 4 - Database with Flask-SQLAlchemy

Python Flask Tutorial: Full-Featured Web App Part 4 - Database with Flask-SQLAlchemy

Corey Schafer

6 лет назад

658,834 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Sista Kishor
Sista Kishor - 18.09.2023 17:07

I am facing this problem could any one solve.... I hold here
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

Ответить
Michael Eduardo Sandoval Pérez
Michael Eduardo Sandoval Pérez - 26.08.2023 01:45

For those who have problems when using: db.create_all()


from flaskblog import db, app

app.app_context().push()

db.create_all()

.
.
.

the rest of the code should work

Ответить
@jayseb
@jayseb - 11.08.2023 15:13

Try as I might, the db stuff just doesn't fly in 2023. Great video, just needs an update I guess.

Ответить
M7MD ZILLA
M7MD ZILLA - 28.07.2023 16:52

i keep geting ModuleNotFoundError: No module named 'flaskblog' i looked for a fix way in the comment and even in other tutorials but it is the same

Ответить
Jonathan Goode
Jonathan Goode - 24.07.2023 18:45

I hope that one day there is a newer version of this. Everything was going great until I got to the sql lite stuff, lol. I have tried using with app.app_context for each of the commands, but it only responds with a string version of the command, even if the command includes print statements, it doesn't even print an error. I may have to abandon this for a different tutorial. But so far it has has been great, and some of the stuff afterwards seems like it works to. just can't get the database part to work.

Ответить
Alexey Zaytsev
Alexey Zaytsev - 17.07.2023 10:36

Would love to see an update version of this series. Or maybe FastAPI instead of Flask? I think the two are pretty similar.

Ответить
Anastasia Klimenko
Anastasia Klimenko - 30.06.2023 12:04

hey all!
as long as "The relationship.backref keyword should be considered legacy, and use of 
relationship.back_populates with explicit relationship() constructs should be preferred. "
that's what I changed in models:
class User(db.Model):
# changed posts line:
posts = db.relationship('Post', back_populates='author')

class Post(db.Model):
# added line
author = db.relationship('User', back_populates='posts')

to see in Terminal those relationship attributes:
app.app_context().push()
u = User.query.filter_by(id=1).first()
u.posts
all_posts = Post.query.all()
all_posts[0].author

Ответить
Zemtsovski Dreamworks
Zemtsovski Dreamworks - 16.06.2023 12:19

Hi all, found problem with creating DB. Im working in pycharm and when i type in terminal "from project import app, db",i get error "The 'from' keyword is not supported in this version of the language.". How i can fix it ?

Ответить
George Seletski
George Seletski - 17.05.2023 19:00

thnx buddy for recording such videos!

Ответить
Yasin Frough
Yasin Frough - 06.05.2023 19:04

Thank you so much these amazing explanations.

Ответить
Sahil Nath
Sahil Nath - 19.04.2023 12:02

I'm facing this error "sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers." while trying to create user_1
please help.

Ответить
Wilson Cueva
Wilson Cueva - 31.03.2023 03:01

thank you very much for your tutorial. its excelent.

Ответить
erdoganb
erdoganb - 27.03.2023 09:43

i don't have the coloring in vscode. for example db.Column(...) the function Column seen as white. But in Corey's screen its orange. How can i do that? :)

Ответить
omkar dedge
omkar dedge - 16.03.2023 06:34

getting error like user_id column is not present in post table

Ответить
Daniel Warzecha
Daniel Warzecha - 06.03.2023 23:36

Hey All: As of version SQLAlchemy 3.0 to create your db file you will need to run some commands like this in the shell :
from project import app, db
app.app_context().push()
db.create_all()

Then the .db file is created in a folder called "Instance" in your project.

Hope this helps some one

Ответить
C. D
C. D - 04.03.2023 05:49

How were you able to run those commands to create items in the database, without the application context issues? I know this is old, so maybe that is the answer, but still I was not able to do any of those commands without "with app.app_context()", and even then I still could not seem to add anything to the db.

Regardless, I really appreciate your series here, overall very helpful.

Ответить
Klaus Codes
Klaus Codes - 02.03.2023 21:18

I have the same error @Othmane describes below and in my terminal, once I write the command my terminal tells me that 'from is not a recognized as an internal or external command'

Ответить
Steven Andrews
Steven Andrews - 20.02.2023 17:17

Hi team,

I'm wondering if anyone has come across this issue yet. I've tried to run 'from flaskblog import db' command, which returns some objections and this error message

"sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqllite"

Has anyone come across this before and if so, please can you share how you resolved the issue.

Ответить
Luke
Luke - 28.01.2023 14:00

This video really helped me a lot, everything is clear now tysm Corey !

Ответить
Technical Student Stuff
Technical Student Stuff - 09.01.2023 16:21

I am unable to do db.create_all()

Ответить
:
: - 06.01.2023 17:56

Does anyone had this error, using the python command at first, when typing db.create_all()?

[...]
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

Ответить
Anthony Hind
Anthony Hind - 30.12.2022 22:40

Hey probably a really bad nuub question but working with the db and getting some errors any thoughts in this ?

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

Ответить
sebastian Marrera
sebastian Marrera - 20.11.2022 22:24

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

I got this with the db.session.add()

Before that, i added the:

```
with app.app_context():
db.create_all()
```

Ответить
yazid kurdi
yazid kurdi - 17.11.2022 17:11

For anyone confused why (User.query.all()) is not returning any values, try this:

with app.app_context():
db.session.add(user_1)
db.session.add(user_2)
db.session.commit()
User.query.all()

Ответить
Othmane ŤheBold
Othmane ŤheBold - 14.11.2022 21:04

For anyone who is facing the error of 'outside application context', try this :
- in your terminal: from <your_app> import app, db
- with app.app_context():
db.create_all()

Note: You can use this also when you will want to add data to the database. GoodLuck!

Ответить
Isaac Fearn
Isaac Fearn - 13.11.2022 20:56

When attempting to create the database with db.create_all() this error comes up

RuntimeError: Working outside of application context.

How do I fix this?

Ответить
Sonu Bose
Sonu Bose - 05.11.2022 19:07

I am getting this error while creating db model in 4th module. Any help please

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqllite

Ответить
Coding With Kamrul
Coding With Kamrul - 05.11.2022 07:22

It is showing an error out of application context. Would you please solve it?

Ответить
Harsh Upreti
Harsh Upreti - 25.10.2022 22:06

Love this guy !

Ответить
Mudassir Aqeel
Mudassir Aqeel - 22.10.2022 15:43

I'm getting this error, when I try to run db.create_all() in terminal:

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

Ответить
Kahi chalte hain...
Kahi chalte hain... - 18.10.2022 16:36

db.create_all() did not work for me, it shows this
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

what should I do?

Ответить
mm
mm - 14.10.2022 13:40

spending 3 hours to find solution to err : works outside of application context :)

Ответить
Meet Mehta
Meet Mehta - 11.10.2022 17:39

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!HELP NEEDED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I cannot create database element using create_all() function in python terminal
It gives an error of Working outside of application context.
Please help anyone if you have resolved this issue.
Thank you.

Ответить
Omar Fessi
Omar Fessi - 10.10.2022 01:04

thanks Corey, this is even better than paid courses !
Is there a way to create stored procedures and triggers in a OOP way with SQLAlchemy the way you did with models ?

Ответить
Paul Cal
Paul Cal - 07.10.2022 23:01

I am getting an error when I try to db.create_all(). The error is: "RuntimeError: Working outside of application context". This even happens when I try to run the source code. Any idea why?

Ответить
Ayomal UH
Ayomal UH - 06.10.2022 19:59

for anyone whose having an issue with db.create_all(), use the following commands after watching the next video.
(env) C:.\..\..\flask app\Flask_blog>set FLASK_APP=run
(env) C:.\..\..\flask app\Flask_blog>flask shell
>>> from flaskblog import db
>>> from flaskblog.models import User, Post
>>>db.create_all()

Ответить
Kenneth Chan
Kenneth Chan - 06.10.2022 10:06

Anyone know how to fix the runtime error "Working outside of application context" when trying to run db.create_all()? please pm me <3

Ответить
Ayomal UH
Ayomal UH - 05.10.2022 06:13

If anyone got a 'working outside of application context' error when running db.create_all() ?

Ответить
Andre -
Andre - - 23.09.2022 17:39

Hey, I have some problems with my python program.
I already have a database in MySQL and don't know how to connect it with almost the same WTForms you used. Can you explain how to connect the WTForms to an already existing database in MySQL?

Ответить
saurabh ruikar
saurabh ruikar - 11.09.2022 13:13

In the User class wthout using a constructor (__init__(self):) how can username,email etc became class variables

Ответить
Scyther Alpha
Scyther Alpha - 11.09.2022 09:15

Which color theme are you using sir?

Ответить
erasemelater
erasemelater - 08.09.2022 18:01

If someday I will decide to move such project online to some server, should I treat database something differently or I can keep that site.db file in the root folder of a project and connect to that database as shown here?

Ответить
Zainab Mahmud
Zainab Mahmud - 02.09.2022 15:09

Whenever I try to access the user_id from the post, I get None. Could anyone please help me with this?

Ответить
Elena Perez
Elena Perez - 30.08.2022 16:11

Great Flask tutorials, thank you! Can I ask why use a SQLAlchemy db for dev and a PostgreSQL db for prod?

Ответить
Daniel Guyatt
Daniel Guyatt - 16.08.2022 14:18

Could anyone explain what I would need to do differently if I wanted to use PostgreSQL? Is it a simple case of changing the URI in the configuration?

Ответить