from my_project import db
from my_project import User, Post
# Create the database
db.create_all()
# Add users to database
user_1 = User(username="Stefan", email="S@demo.com", password="password")
user_2 = User(username="Lily", email="L@demo.com", password="password")
db.session.add(user_1)
db.session.add(user_2)
db.session.commit()
# Query database
User.query.all() #returns all users
User.query.first() # returns first user added
User.query.filter_by(username="Stefan").all() # show all users named Stefan
user = User.query.filter_by(username="Stefan").first() # show first user named Stefan
user.id # returns integer of 1
user = User.query.get(1) # select by user id; same user