Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2013 14:38
Show Gist options
  • Save anonymous/5254675 to your computer and use it in GitHub Desktop.
Save anonymous/5254675 to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker, backref
from sqlalchemy import event
#enforce foreign keys, snippet from SO
def _fk_pragma_on_connect(dbapi_con, con_record):
dbapi_con.execute('pragma foreign_keys=ON')
event.listen(engine, 'connect', _fk_pragma_on_connect)
engine = create_engine('sqlite:///lexi.db', echo=True)
Base = declarative_base()
class Words(Base):
__tablename__ = 'words'
word = Column(String, primary_key=True)
defination = relationship("Definations", backref='words')
def __init__(self, word):
self.word = word
def __repr__(self):
return "<Words('%s')>" % self.word
class Definations(Base):
__tablename__ = 'definations'
id = Column(Integer, primary_key=True)
word = Column(String, ForeignKey('words.word'))
part_of_speech = Column(String)
pronunciation = Column(String)
defination = Column(String)
def __init__(self, part_of_speech, pronunciation, defination):
self.part_of_speech = part_of_speech
self.pronunciation = pronunciation
self.defination = defination
def __repr__(self):
return "<Definations('%s', '%s', '%s')>" % (self.part_of_speech,
self.pronunciation,
self.defination)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment