Skip to content

Instantly share code, notes, and snippets.

@JonnoFTW
Created February 21, 2020 02:45
Show Gist options
  • Save JonnoFTW/503edbb0eee76771bba8f2579e9e6a88 to your computer and use it in GitHub Desktop.
Save JonnoFTW/503edbb0eee76771bba8f2579e9e6a88 to your computer and use it in GitHub Desktop.
sqlalchemy has no base
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from models import User
def create_app(config=None):
app = Flask("testing")
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/')
def default():
return "hello to " + ", ".join(User.query.all())
return app
# this file is in the models/ folder
from sqlalchemy import Column, String
from app import db
class User(db.Base):
__tablename__ = "user"
name = Column(String, nullable=False)
def __repr__(self):
return f"<User name={self.name}>"
def init_data(db):
db.session.add_all([
User("frank"),
User("jim"),
User("paul")
])
db.session.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment