Hi folks. I have worked my way through the association and association
proxy examples and almost got what I want going, but I'm stuck on a
couple of points.
I have an assoc table with two foreign keys and one extra numerical
field. In the example in the docs, the third extra field is a third
foreign key to a table instead of an integer, ( in that case it is for
Users )
I see how the example grabs the user data by using:
mapper(KeywordAssociation, itemkeywords_table,
primary_key=[itemkeywords_table.c.article_id,
itemkeywords_table.c.keyword_id],
properties={
'keyword' : relation(Keyword, lazy=False),
'user' : relation(User, lazy=False)
}
)
which can the be accessed directly using the association proxy off of
class Article(object):
keywords = AssociationProxy('keyword_associations', 'keyword')
thus allowing us to use: article.keyword.keyword_name
instead of: article.keyword_association.keyword.keyword_name
I'm trying to figure out how to do the same thing with an extra property that
is not a foreign key.
So that I can do article.ordering where ordering is the integer key in the
assoc table.
Below is my code in case someone has time to look at it. At the moment it works
to get
article.ordering as a list instead of one field. Is there a way to tell it that
this is only supposed to be one item?
Thanks
Iain
page_table = Table('pages', metadata,
Column('page_id', Integer, primary_key = True),
Column('page_name', String(50))
)
article_table = Table('articles', metadata,
Column('article_id', Integer, primary_key = True),
Column('article_name', String(150) ),
)
page_article_table = Table('page_article', metadata,
Column('page_id', Integer, ForeignKey("pages.page_id")),
Column('article_id', Integer, ForeignKey("articles.article_id")),
Column('ordering', Integer ),
)
# class definitions
class Page(object):
def __init__(self, name):
self.page_name = name
# create articles proxied association
articles = AssociationProxy('article_associations', 'article')
class Article(object):
def __init__(self, name):
self.article_name = name
# this works to get the ordering field but gets it as a list instead of one
int
ordering = AssociationProxy('ordering_association', 'ordering')
class PageArticleAssoc(object):
pass
# Page mapper, relates to articles via Association object
mapper(Page, page_table, properties={
'article_associations':relation(PageArticleAssoc, lazy=False, cascade="all,
delete-orphan")
}
)
# keyword mapper
mapper(Article, article_table, properties={
'pages':relation(PageArticleAssoc, lazy=False, cascade="all,
delete-orphan"),
}
)
# mapper for PageArticleAssoc
mapper( PageArticleAssoc, page_article_table,
primary_key= [ page_article_table.c.page_id,
page_article_table.c.article_id ],
properties={
'article' : relation(Article, lazy=False),
'page' : relation(Page, lazy=False),
}
)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---