Skip to content

Instantly share code, notes, and snippets.

@thequbit
Created September 12, 2013 03:12
Show Gist options
  • Save thequbit/6532687 to your computer and use it in GitHub Desktop.
Save thequbit/6532687 to your computer and use it in GitHub Desktop.
from sqlalchemy import (
Column,
ForeignKey,
Integer,
Text,
)
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
)
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy.ext.declarative import declarative_base
############# Models #############
Base = declarative_base()
class JobTypeModel(Base):
__tablename__ = 'jobtypes'
id = Column(Integer, primary_key=True)
name = Column(Text)
description = Column(Text)
def __init__(self,name,description):
self.name = name
self.description = description
class PersonModel(Base):
__tablename__ = 'people'
id = Column(Integer, primary_key=True)
name = Column(Text)
jobtypeid = Column(Integer, ForeignKey('jobtypes.id'))
def __init__(self,name,jobtypeid):
self.name = name
self.jobtypeid = jobtypeid
################# Code ###############
def main():
engine = create_engine('sqlite:///people.sqlite')
Session = sessionmaker(bind=some_engine)
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
DBSession.configure(bind=engine)
jobtype = JobTypeModel("janitor","takes care of janitoring")
DBSession.add(jobtype)
person = PersonModel("Tim",jobtype.id)
DBSession.add(person)
print "done."
@thequbit
Copy link
Author

jobtype = JobTypeModel("janitor","takes care of janitoring")

DBSession.add(jobtype)

person = PersonModel("Tim",jobtype.id)

DBSession.add(person)

DBSession.commit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment