Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jan 22nd, 2011  |  syntax: Python  |  size: 1.33 KB  |  hits: 82  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from sqlalchemy import *
  2. from sqlalchemy.orm import *
  3. engine = create_engine('sqlite://')
  4. metadata = MetaData()
  5. metadata.bind = engine
  6. Session = scoped_session(sessionmaker(bind=metadata.bind))
  7. session = Session()
  8.  
  9. authors = Table('authors', metadata,
  10.     Column('id', Integer, primary_key=True),
  11.     Column('first_name', String(60)),
  12.     Column('last_name', String(60)))
  13.  
  14. class Author(object):
  15.     pass
  16.  
  17. mapper(Author, authors)
  18.  
  19.  
  20. books = Table('books', metadata,
  21.     Column('id', Integer, primary_key=True),
  22.     Column('title', String(30)),
  23.     Column('author_id', Integer, ForeignKey('authors.id')))
  24.  
  25. class Book(object):
  26.     pass
  27.  
  28. mapper(Book, books, properties={
  29.     'author': relation(Author, backref='books')
  30. })
  31.  
  32.  
  33. metadata.create_all()
  34.  
  35.  
  36. from fixture import SQLAlchemyFixture
  37. dbfixture = SQLAlchemyFixture(
  38.     env={'BookData': Book, 'AuthorData': Author},
  39.     engine=metadata.bind)
  40.  
  41. from fixture import DataSet
  42. class AuthorData(DataSet):
  43.     class frank_herbert:
  44.         first_name = "Frank"
  45.         last_name = "Herbert"
  46.  
  47. class BookData(DataSet):
  48.     class dune:
  49.         title = "Dune"
  50.         author = AuthorData.frank_herbert
  51.  
  52.  
  53. data = dbfixture.data(AuthorData, BookData)
  54. data.setup()
  55.  
  56.  
  57. print data.AuthorData.frank_herbert.id
  58. print data.BookData.dune.author.id == data.AuthorData.frank_herbert.id