Created
February 1, 2012 16:08
-
-
Save Gohan/1717750 to your computer and use it in GitHub Desktop.
sqlalchemy test IMDB cast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from sqlalchemy import * | |
from sqlalchemy.orm import relationship, Session | |
from sqlalchemy.ext.declarative import declarative_base | |
metadata = MetaData() | |
Base = declarative_base(metadata = metadata) | |
class Member(Base): | |
__tablename__ = 'members' | |
id = Column('id', Integer, primary_key=True) | |
name = Column('username', String) | |
reviews = relationship('Review') | |
class Movie(Base): | |
__tablename__ = 'movies' | |
id = Column('id', Integer, primary_key=True) | |
title = Column('title', String) | |
reviews = relationship('Review') | |
cast_entries = relationship('CastEntry') | |
class Review(Base): | |
__tablename__ = 'reviews' | |
id = Column('id', Integer, primary_key=True) | |
title = Column('title', String) | |
member_id = Column(Integer, ForeignKey('members.id')) | |
movie_id = Column(Integer, ForeignKey('movies.id')) | |
class CastType(Base): | |
__tablename__ = 'cast_types' | |
id = Column('id', Integer, primary_key=True) | |
typename = Column('typename', String) | |
cast_entries = relationship('CastEntry') | |
class CastEntry(Base): | |
__tablename__ = 'cast_entries' | |
id = Column('id', Integer, primary_key=True) | |
cast_type = Column(Integer, ForeignKey('cast_types.id')) | |
person_id = Column(Integer, ForeignKey('persons.id')) | |
movie_id = Column(Integer, ForeignKey('movies.id')) | |
class Person(Base): | |
__tablename__ = 'persons' | |
id = Column('id', Integer, primary_key=True) | |
name = Column('name', String) | |
cast_entries = relationship('CastEntry') | |
if __name__ == '__main__': | |
engine = create_engine('sqlite:///database.db', echo=True) | |
Base.metadata.create_all(engine) | |
session = Session(engine) | |
cast_type_director = CastType() | |
cast_type_director.typename = 'director' | |
cast_type_actor = CastType() | |
cast_type_actor.typename = 'actor' | |
person1 = Person() | |
person1.name = 'actor1' | |
person2 = Person() | |
person2.name = 'd2' | |
session.add_all([cast_type_director, cast_type_actor, person1, person2]) | |
member = Member() | |
member.name = 'cppgohan' | |
movie = Movie() | |
movie.title = 'movie001' | |
review = Review() | |
review.title = 'cppgohan\'s movie001 review1' | |
member.reviews.append(review) | |
movie.reviews.append(review) | |
entries = [] | |
cast_entry = CastEntry() | |
cast_type_director.cast_entries.append(cast_entry) | |
person1.cast_entries.append(cast_entry) | |
movie.cast_entries.append(cast_entry) | |
entries.append(cast_entry) | |
cast_entry = CastEntry() | |
cast_type_actor.cast_entries.append(cast_entry) | |
person2.cast_entries.append(cast_entry) | |
movie.cast_entries.append(cast_entry) | |
entries.append(cast_entry) | |
session.add_all([member, movie, review]) | |
session.add_all(entries) | |
session.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment