All pastes #584139 Raw Edit

Something

public python v1 · immutable
·published 2007-06-22 16:51 UTC
rendered paste body
try:	tbl_photos = Table('photos', metadata, autoload=True)except exceptions.NoSuchTableError:	tbl_photos = Table('photos', metadata,		Column('photo_id', Integer, primary_key=True),		Column('title', String(255)),		Column('comment', String(65535))	)	tbl_photos.create()try:	tbl_tags = Table('tags', metadata, autoload=True)except exceptions.NoSuchTableError:	tbl_tags = Table('tags', metadata,		Column('tag_id', Integer, primary_key=True),		Column('key', String(100)),		Column('value', String(256))	)	tbl_tags.create()try:	tbl_photos_tags = Table('photos_tags', metadata, autoload=True)except exceptions.NoSuchTableError:	tbl_photos_tags = Table('photos_tags', metadata,		Column('photo_tag_id', Integer, primary_key=True),		Column('tag_id', Integer, ForeignKey('tags.tag_id')),		Column('photo_id', Integer, ForeignKey('photos.photo_id')),	)	tbl_photos_tags.create()try:	tbl_albums = Table('albums', metadata, autoload=True)except exceptions.NoSuchTableError:	tbl_albums = Table('albums', metadata,		Column('album_id', Integer, primary_key=True),		Column('title', String(80)),		Column('parent_album_id', Integer, ForeignKey('albums.album_id'), nullable=True),	)	tbl_albums.create()try:	tbl_albums_tags = Table('albums_tags', metadata, autoload=True)except exceptions.NoSuchTableError:	tbl_albums_tags = Table('albums_tags', metadata,		Column('albums_tags_id', Integer, primary_key=True),		Column('album_id', Integer, ForeignKey('albums.album_id')),		Column('tag_id', Integer, ForeignKey('tags.tag_id'))	)	tbl_albums_tags.create()class DBPhoto(object):	def __init__(self, title, comment):		self.title = title		self.comment = comment	def __repr__(self):		return "%s(%r,%r,%r)" % (self.__class__.__name__, self.photo_id, self.title, self.comment)class DBTag(object):	def __init__(self, key = '', value = ''):		self.key = key		self.value = value	def __repr__(self):		return "%s(%r,%r,%r)" % (self.__class__.__name__, self.tag_id, self.key, self.value)class DBAlbum(object):	def __init__(self, title = ''):		self.title = title	def __repr__(self):		return "%s(%r,%r,%r)" % (self.__class__.__name__, self.album_id, self.title, self.parent_album_id)	mapper(DBPhoto, tbl_photos, properties = {	'tags':relation(DBTag, secondary=tbl_photos_tags, lazy=False)	}) mapper(DBTag, tbl_tags, properties = {	'photos':relation(DBPhoto, secondary=tbl_photos_tags, lazy=False)	}) mapper(DBAlbum, tbl_albums, properties = {	'tags':relation(DBTag, secondary=tbl_albums_tags, lazy=False), 	'photos':relation(DBPhoto, secondary=tbl_albums_tags, primaryjoin=tbl_albums.album_id==tbl_albums_tags.album_id, secondaryjoin=tbl_albums_tags.tag_id==tbl_tags.tag_id),	'children':relation(DBAlbum, cascade='all')	})