Skip to content

Instantly share code, notes, and snippets.

@klrkdekira
Forked from anonymous/sqlalchemy.py
Last active December 15, 2015 11:39
Show Gist options
  • Save klrkdekira/5254762 to your computer and use it in GitHub Desktop.
Save klrkdekira/5254762 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, scoped_session
DBSession = scoped_session(sessionmaker())
Base = declarative_base()
class Words(Base):
__tablename__ = 'words'
word = Column(String, primary_key=True)
def __repr__(self):
return "<Words('%s')>" % self.word
class Definition(Base):
__tablename__ = 'definitions'
id = Column(Integer, primary_key=True)
word_id = Column(String, ForeignKey('words.word', ondelete='CASCADE', onupdate='CASCADE'))
part_of_speech = Column(String)
pronunciation = Column(String)
definition = Column(String)
word = relationship('Words', uselist=False, backref='definitions')
def __repr__(self):
return "<Definations('%s', '%s', '%s')>" % (self.part_of_speech,
self.pronunciation,
self.defination)
if __name__ == '__main__':
engine = create_engine('sqlite:///lexi.db', echo=True)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
word = Words(word='hello')
DBSession.add(word)
d1 = Definition(part_of_speech='pos1',
pronunciation='p1',
definition='d1',
word=word)
DBSession.add(d1)
d2 = Definition(part_of_speech='pos2',
pronunciation='p2',
definition='d2')
DBSession.add(d2)
word.definitions.append(d2)
d3 = Definition(part_of_speech='pos3',
pronunciation='p3',
definition='d3',
word_id=word.word)
DBSession.add(d3)
DBSession.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment