I've made a simple code using python, where with the help of Flask_sqlalchemy I am trying to create 2 tables in a database which is being hosted by Heroku.
The file db.py contains my table details
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Flight(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): __tablename__ = "passengers" 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)
And another file called test.db to call db.py
import os from flask import Flask, render_template, request from model import * app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "postgres://neakxnxnwavgux:firstname.lastname@example.org:5432/doa4533066h0a" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db.init(app) def main(): db.create_all() if __name__ == "__main__": with app.app_context(): main()
db.init(app) NameError: name 'db' is not defined
I had an import error saying that models doesn't exist and ran the code
pip install pymodel in terminal .
But now im stuck with a bigger problem that doesnt allow me to continue.