0
votes

new to web developing. I have three files. database_setup.py with my table classes. restaurant.py used to populate tables and script.py for flask app

database_setup.py

import os

import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class Restaurant(Base):
    __tablename__ = 'restaurant'

    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)         

engine = create_engine('sqlite:///restaurantmenu.db')


Base.metadata.create_all(engine)

restaurant.py

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from database_setup import Restaurant, Base, MenuItem

engine = create_engine('sqlite:///restaurantmenu.db')

Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)

session = DBSession()


restaurant = Restaurant(name= "Kasra")
session.add(restaurant)
session.commit()

script.py

from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker,scoped_session
from database_setup import Base, Restaurant

app = Flask(__name__)

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine

DBSession = scoped_session(sessionmaker(bind=engine))
session = DBSession()

@app.route('/')
@app.route('/hello')
def HelloWorld():
    restaurant = session.query(Restaurant).first()
    return restaurant.name


if __name__ == "__main__":
    app.run(debug=True)

What I want to do is import the database that was created in restarant.py to script.py. When I run the app it creates a new and empty database because I'm using "engine = "create_engine()" in script.py and I need to use it in order to bind with engine and use session.

The error I get when running app "AttributeError: 'NoneType' object has no attribute 'name'"

1
Please share full exception log or stack trace.Ejaz
You certainly only want one, engine = create_engine(...) call in your code. In both restaurant.py and script.py, the only interaction that you have with the database is through the session, so construct your scoped_session instance in database_setup.py and import the session into both restaurant.py and script.py.SuperShoot
So import engine from database_setup.py to both files?user10108489

1 Answers

0
votes

Here is what I do to operate with Flask and SQLAlchemy:

In a database.py file:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base

engine = create_engine(Config.SQLALCHEMY_DATABASE_URI)
session_factory = sessionmaker(bind=engine)

def init_db():
    Base.metadata.create_all(bind=engine)

All model classes are located in a models.py file, and are also derived from the declarative base:

Base = declarative_base()

Then the start-up python script contains (key parts only):

app = Flask(__name__)
session = flask_scoped_session(session_factory, app)

if __name__ == "__main__":
    database.init_db()
    app.run()