Python Flask Tutorial: Full-Featured Web App Part 4 - Database with Flask-SQLAlchemy
Why Stack Overflow recommends it
Stack Overflow users cite it as the tutorial they were following when their Flask-SQLAlchemy setup, such as db.create_all(), broke or threw errors.
Recommended by 0 people across 0 different months, 2018 to 2023.
What it is
Part 4 of Corey Schafer's Flask web app series, a roughly 30 minute tutorial on setting up a database with Flask-SQLAlchemy. It builds User and Post models with SQLite, covers relationships, and shows how to create and query tables from the Python shell.
What people said
Cites TermSpar · 2018-11 · Stack Overflow · on “Python Flask SQLite OperationalError when viewing posts of a user”
I'm currently following this [this video] tutorial series on how to build a website with Flask, however I'm running across a very frustrating error when I'm just following exactly what the guy is doing. Right now, I have the following classes setup for the database to access: class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.us
Cites Kevin Wilczynski · 2020-04 · Stack Overflow · on “Trouble adding comment relation to User table in flask-sqlalchemy”
so I just got done following python YouTuber Corey Schafers tutorial on flask-sqlalchemy. I'm having problems modifying the DB we created. I'm trying to make a relation to my Comment table and User table. When a user posts a comment I want to store the user's name. So far I can't seem to link a user's comment on a post to the name of the author of the comment. This is the code that's relevant. from datetime import datetime from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from smushd_site import db, login_manager from flask_login import UserMixin from flask import current_app @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) class Use
Cites MangoHen · 2020-05 · Stack Overflow · on “Error when running flask and sql alchemy together”
I keep getting these errors when I try to run my application guessing it has something to do with how I imported sqlalchemy but couldn't put a pin in it I am following this tutorial: [this video] these are the errors it gives me enter image description here [LINK] all of them are something along the lines of instance of sqlalchemy has no column member here is my code: from datetime import datetime from flask import Flask, render_template, url_for, flash, redirect from flask_sqlalchemy import SQLAlchemy from forms import RegistrationForm, LoginForm app = Flask(__name__) app.config['SECRET_KEY'] = 'thesecretkey' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db=SQLAlchemy(app) class Us
Cites Smasher640 · 2020-06 · Stack Overflow · on “Flask: ModuleNotFoundError: No module named 'flask_sqlalchemy'”
I'm following this popular tutorial [this video] to make a Flask Web App. I am doing this in Visual Code Studio, but I am encountering an error with SQLAlchemy. from flask_sqlalchemy import SQLAlchemy This is my code for importing the module. I am receiving "ModuleNotFoundError: No module named 'flask_sqlalchemy'". I have checked that I have correctly installed flask_sqlalchemy, by entering 'pip list' in CMD, inside my project folder, and it is listed under the installed packages. So I am confused about why VScode is unable to find the module. After searching online a potential problem seems to be with virtual environments, but I seem to have correctly installed the module locally for the project
Cites kbizzle · 2020-07 · Stack Overflow · on “SQLAlchemy db.create_all() Error, not creating db”
I am stuck at trying to create a db following a flask tutorial: [this video] I've unsuccessfully search for the solution to this issue. Please help. When I try to run db.create_all() after importing db from my project, I get sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file Python 3.8.3 from datetime import datetime from flask import Flask, render_template, url_for, flash, redirect from flask_sqlalchemy import SQLAlchemy from forms import RegistrationForm, LoginForm app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.S
Cites Dark Lei · 2020-08 · Stack Overflow · on “Flask syntax f-string with single quotation mark”
In a Flask tutorial, [this video] There is a syntax I want to understand return f"Post('{self.title}', '{self.dateposted}')" Can someone decompose this line for me? f string means to return the result of Post() in a string '{self.title}' is an argument that is a string, because with '' {} is used to get a variable. But in the context self.title is ALREADY a string, why use ' ' ? Are my explanations correct? And what does ' ' do here?
Cites AndreyIto · 2021-03 · Stack Overflow · on “Flask SQLAlchemy can't set attribute error in a venv”
I'm learning authentication functionality with Flask-SQLAlchemy , so building out very basic apps where a user (attributes: name, email and password) is able to sign-up and login, get redirected to a dummy profile page, and that's it. The puzzling behaviour is the tutorials I've done won't work all the way through if I use a virtual environment. To show this behaviour, follow this digital ocean tutorial [LINK] . At step 6, we have: >>> from project import db, create_app >>> db.create_all(app=create_app()) If I have the auth environment active (as the tutorial states I should), I get the following error message: /.../flask_auth_app/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py
Cites erdoganb · 2023-03 · Stack Overflow · on “RuntimeError: Working outside of application context. Corey Schafer Flask Tutorial”
i'm trying to understand flask. I'm a newbie. I made progress up to lesson 4. [this video] Create templates, static-css, bootstrap connections, ORM classes. When i open the python terminal and from flaskblog import db it is ok, no statement like in the vide, then db.create_all() this is where my code collapse... error is exactly as it is: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/erdoganb/Library/Python/3.9/lib/python/site-packages/flask_sqlalchemy/extension.py", line 884, in create_all self._call_for_binds(bind_key, "create_all") File "/Users/erdoganb/Library/Python/3.9/lib/python/site-packages/flask_sqlalchemy/extension.py", line 855, in _call_for_binds