0

While I was trying to run create.py file having the following code:

import os

from flask import Flask, render_template, request
from models import *

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URL"] = os.getenv("DATABASE_URL")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)

def main():
    db.create_all()

if __name__ == "__main__":
    with app.app_context():
        main()

and models.py had following code:

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class flights(db.Model):
    __tablename__ = "flights"
    id = db.Column(db.Integer, primary_key=True)
    origin = db.Column(db.String, nullable=False)
    destination = db.Column(db.String, nullable=False)
    duration = db.Column(db.Integer, nullable=False)

class Passenger(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    flight_id = db.Column(db.Integer, db.ForeignKey("flights.id"), nullable=False)

The following error was returned by command line when I used the command python create.py:

UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:". 'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '

I have already set DATABASE_URL equal to my database linked with https://heroku.com.

Please help me out with some solution. I am using Windows 10 and VS Code as code editor.

0

I solved it myself; I needed to set DATABASE_URL='postgres//:....' and then run the app.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .