Skip to content

Instantly share code, notes, and snippets.

@cruor99
Last active April 24, 2016 00:40
Show Gist options
  • Select an option

  • Save cruor99/1ad6d2845a478b58c8bdf00656e177e8 to your computer and use it in GitHub Desktop.

Select an option

Save cruor99/1ad6d2845a478b58c8bdf00656e177e8 to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('sqlite:///./database.db')
Session = sessionmaker(bind=engine)
db = Session()
class Polls(Base):
__tablename__ = "polls"
id = Column(Integer, primary_key=True)
question = Column(String)
class PollOptions(Base):
__tablename__ = "polloptions"
id = Column(Integer, primary_key=True)
text = Column(String)
pollid = Column(Integer, ForeignKey("polls.id"))
polls = relationship("Polls")
class Votes(Base):
__tablename__ = "votes"
id = Column(Integer, primary_key=True)
voterip = Column(String)
polloptionid = Column(Integer, ForeignKey("polloptions.id"))
polloptions = relationship("PollOptions")
if __name__ == "__main__":
# create sqlite db
# Base.metadata.create_all(engine)
pollquery = db.query(PollOptions).filter_by(id="1")
pollqueryid = pollquery.first().id
print len(pollquery.all())
print pollquery
print "======"
polloptions = db.query(PollOptions).filter_by(pollid=pollqueryid)
print polloptions
print "======"
votequery = db.query(Votes).filter_by(polloptionid=polloptions.first().id).filter_by(voterip="::1")
print votequery
print len(votequery.all())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment