Skip to content

Instantly share code, notes, and snippets.

@guyjacks
Created May 19, 2015 17:19
Show Gist options
  • Save guyjacks/b623e382fc99901f0464 to your computer and use it in GitHub Desktop.
Save guyjacks/b623e382fc99901f0464 to your computer and use it in GitHub Desktop.
-- database.py --
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
basedir = os.path.abspath(os.path.dirname(__file__))
engine = create_engine(
'sqlite:///' + os.path.join(basedir, 'app.db'),
convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
import theatertilt.models
Base.metadata.create_all(bind=engine)
-- models.py --
from sqlalchemy import Column, Integer, String, DateTime, Date, Boolean, ForeignKey
from sqlalchemy.orm import relationship, backref
from theatertilt.database import Base
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
first = Column(String(25))
last = Column(String(25))
email = Column(String(255))
zip = Column(String(5))
dob = Column(Date)
# 0 is male, 1 is female
gender = Column(Boolean)
tickets = relationship('Ticket', backref='user')
-- query that is not working --
User.query.filter_by(email='test@example.com').id()
-- query that IS working --
User.query.all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment