Created
February 21, 2020 02:45
-
-
Save JonnoFTW/503edbb0eee76771bba8f2579e9e6a88 to your computer and use it in GitHub Desktop.
sqlalchemy has no base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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