Read Sean

          Read me, read Sean.
          posts - 508, comments - 655, trackbacks - 9, articles - 4

          [Pylons] SQLAlchemy起步 - IV. Object Relational Mapper

          Posted on 2009-01-27 12:52 laogao 閱讀(1696) 評論(0)  編輯  收藏 所屬分類: On Python

          接著前面的例子說,我們定義了book_table和author_table,接下來:
          ?1?class?Book(object):
          ?2?????def?__init__(self,?title):
          ?3?????????self.title?=?title
          ?4?????def?__repr__(self):
          ?5?????????return?"<Book('%s')>"?%?self.title
          ?6?
          ?7?class?Author(object):
          ?8?????def?__init__(self,?name):
          ?9?????????self.name?=?name
          10?????def?__repr__(self):
          11?????????return?"<Author('%s')>"?%?self.name

          這里我們定義兩個類,繼承自object,類似JavaBeans或者POJO,這里的__init__方法和__repr__方法不是必須的,只是為了創(chuàng)建對象和輸出對象內(nèi)容比較方便。然后就可以用SQLAlchemy的mapper和sessionmaker來建立映射關(guān)系并處理持久和查詢等操作:
          ?1?from?sqlalchemy.orm?import?mapper,sessionmaker
          ?2?
          ?3?mapper(Book,?book_table)
          ?4?mapper(Author,?author_table)
          ?5?
          ?6?Session?=?sessionmaker(bind=engine)
          ?7?session?=?Session()
          ?8?
          ?9?gia?=?Book(u'Groovy?in?Action')
          10?ag?=?Author(u'Andrew?Glover')
          11?
          12?session.add(gia)
          13?session.add(ag)
          14?session.add_all([Book('Hibernate?in?Action'),?Author('Gavin?King')])
          15?s_gia?=?session.query(Book).filter_by(title=u'Groovy?in?Action').first()
          16?s_gia.title?=u'Groovy?in?Action?Updated'
          17?
          18?print?"[DIRTY]",?session.dirty
          19
          20?session.commit() # or session.rollback()

          如果你用過Hibernate,那么這些代碼對你來說,理解起來應該沒有任何難度。

          假如我告訴你,每次都要像這樣先定義Table(schema),再定義class,然后用mapper建立對照,是不是有點那啥?SQLAlchemy的開發(fā)者們也意識到這一點,所以從0.5開始,SQLAlchemy可以通過sqlalchemy.ext.declarative支持我們實現(xiàn)更緊湊的model/schema定義:
          ?1?from?sqlalchemy.schema?import?Table,?Column,?ForeignKey,?Sequence
          ?2?from?sqlalchemy.types?import?*
          ?3?from?sqlalchemy.orm?import?relation
          ?4?from?sqlalchemy.ext.declarative?import?declarative_base
          ?5?
          ?6?Base?=?declarative_base()
          ?7?metadata?=?Base.metadata
          ?8?
          ?9?bookauthor_table?=?Table('bookauthor',?metadata,
          10?????Column('book_id',?Integer,?ForeignKey('book.id'),?nullable=False),
          11?????Column('author_id',?Integer,?ForeignKey('author.id'),?nullable=False),
          12?)
          13?
          14?class?Book(Base):
          15?????__tablename__?=?'book'
          16?????id?=?Column(Integer,?Sequence('seq_pk'),?primary_key=True)
          17?????title?=?Column(Unicode(255),?nullable=False)
          18?????authors?=?relation('Author',?secondary=bookauthor_table)
          19?
          20?
          21?class?Author(Base):
          22?????__tablename__?=?'author'
          23?????id?=?Column(Integer,?Sequence('seq_pk'),?primary_key=True)
          24?????name?=?Column(Unicode(255),?nullable=False)
          25?????books?=?relation('Book',?secondary=bookauthor_table)

          這里我們用到了many-to-many關(guān)系,其他的常見用法還包括many-to-one、one-to-many、JOIN、子查詢、EXISTS、Lazy/Eager Load、Cascade (all/delete/delete-orphan)等等,大家可以根據(jù)需要查閱官方文檔。

          主站蜘蛛池模板: 万荣县| 宣化县| 富阳市| 亚东县| 石泉县| 淅川县| 石家庄市| 来凤县| 宜君县| 右玉县| 万荣县| 修水县| 青岛市| 从化市| 乡城县| 呼图壁县| 乌拉特前旗| 虞城县| 大庆市| 海阳市| 赤壁市| 内乡县| 抚松县| 亳州市| 寿阳县| 遂宁市| 双牌县| 大石桥市| 永和县| 清远市| 东乡| 佛山市| 综艺| 依安县| 太保市| 奉化市| 安化县| 琼结县| 游戏| 固安县| 岢岚县|