]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
(no commit message)
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 20 Oct 2005 05:33:01 +0000 (05:33 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 20 Oct 2005 05:33:01 +0000 (05:33 +0000)
test/rundocs.py

index 8ed8144452ed50f90ec174782725cc3cfcb18b0a..79fd14de9781488d76e77cb6c1210ae4bae6a56e 100644 (file)
@@ -100,9 +100,9 @@ users.insert().execute(
 
 
 addresses = Table('email_addresses', engine,
-    Column('address_id', INT, primary_key = True),
-    Column('user_id', INT, ForeignKey(users.c.user_id)),
-    Column('email_address', VARCHAR(20)),
+    Column('address_id', Integer, primary_key = True),
+    Column('user_id', Integer, ForeignKey(users.c.user_id)),
+    Column('email_address', String(20)),
 )
 addresses.drop()
 addresses.create()
@@ -129,3 +129,60 @@ user.addresses.append(Address('freddy@hi.org'))
 
 # commit
 objectstore.commit()
+
+
+
+articles = Table('articles', engine,
+    Column('article_id', Integer, primary_key = True),
+    Column('article_headline', String(150), key='headline'),
+    Column('article_body', CLOB, key='body'),
+)
+
+keywords = Table('keywords', engine,
+    Column('keyword_id', Integer, primary_key = True),
+    Column('name', String(50))
+)
+
+itemkeywords = Table('article_keywords', engine,
+    Column('article_id', Integer, ForeignKey(articles.c.article_id)),
+    Column('keyword_id', Integer, ForeignKey(keywords.c.keyword_id))
+)
+
+articles.create()
+keywords.create()
+itemkeywords.create()
+
+# class definitions
+class Keyword(object):
+    def __init__(self, name = None):
+        self.name = name
+    mapper = assignmapper(keywords)
+    
+class Article(object):
+    def __init__(self):
+        self.keywords = []
+    mapper = assignmapper(articles, properties = dict(
+        keywords = relation(Keyword.mapper, itemkeywords, lazy=False)
+        ))
+Article.mapper
+
+article = Article()
+article.headline = 'a headline'
+article.body = 'this is the body'
+article.keywords.append(Keyword('politics'))
+article.keywords.append(Keyword('entertainment'))
+objectstore.commit()
+
+# select articles based on some keywords.  the extra selection criterion 
+# won't get in the way of the separate eager load of all the article's keywords
+alist = Article.mapper.select(sql.and_(keywords.c.keyword_id==articles.c.article_id, keywords.c.name.in_('politics', 'entertainment')))
+
+# modify
+a = alist[0]
+del a.keywords[:]
+a.keywords.append(Keyword('topstories'))
+a.keywords.append(Keyword('government'))
+
+# commit.  individual INSERT/DELETE operations will take place only for the list
+# elements that changed.
+objectstore.commit()
\ No newline at end of file