I'm following a CRUD tutorial using Flask and SQL Alchemy.
In the tutorial, when he runs this code, it's created a folder called 'instance' and inside it there's the .db file.
When I do the same, the folder is created but not the file. I checked over and over this code, and it matches with his, so I couldn't find a reason why it's not working.
#Imports
from flask import Flask, render_template
from flask_scss import Scss
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import os
#App
app = Flask(__name__)
Scss(app)
app.config['SQLALCHEMY_DATABASE_URI'] ='sqlite:///database.db'
db = SQLAlchemy(app)
#Data Class
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(length=100),
nullable=False)
complete = db.Column(db.Integer, default=0)
created = db.Column(db.DateTime, default=datetime.now())
def __repr__(self) -> str:
return f"Task {self.id}"
#Routes
@app.route("/")
def index():
return render_template("index.html")
#Runner
if __name__ in "__main__":
with app.app_context():
db.create_all()
app.run(debug=True)
Appreciate the help!
Just tried it myself on windows and it runs flawlessly. And since it can create the folder I'm guessing it's either a problem with permissions to create a .db file:
chmod -R 777 app_directory_nameor something with the relative path:
try including this line in
app.app_context()and check if the printed path is correct
If neither work try creating a fresh virtual environment (
python -m venv venv) or trying on a different OS.