Tml wrote: > > Hi, > > I want to make a relation such that.. i have users post some > articles.. and can link them as relatd to other articles. > > I have the Article table like this: > articles = Table('articles', metadata, > Column('id', Integer, primary_key=True), > Column('topic', Unicode(256), nullable=False), > Column('rank', Integer), > Column('content', Unicode, nullable=False), > mysql_engine='InnoDB' > ) > > And I have a related articles association table like this: > > articles_related_articles = > Column('article_id', Integer, ForeignKey('articles.id'), > index=True, nullable=False), > Column('related_id', Integer, nullable=False), > Column('bias', SmallInteger, default=0), > mysql_engine='InnoDB' > ) >
You probably want to add ForeignKey('articles.id') to the related_id column as well, and add primary_key=True to both article_id and related_id since they identify the row. > > So, if article 1 is related to article 2.. then there should be two > rows in the articles_related_articles table like: > > 1 2 0 > > 2 1 0 > > This is how I make the relation mapping in SA: > > mapper(ArticleRelatedArticle, articles_realted_articles, > primary_key=[articles_realted_articles.c.article_id, > articles_related_articles.c.related_id], > properties={'article': relation(Article),} > ) > > assign_mapper(context, Article, articles, > 'related': relation(ArticlesRelatedArticles, > cascade="all, delete- > orphan", lazy=True) > ) > You're using assign_mapper for one class, and plain mapper for another. I don't know whether that might be part of the confusion. I think I would set up the ArticleRelatedArticle mapper something like this (untested, probably contains mistakes): assign_mapper(context, ArticleRelatedArticle, articles_related_articles, properties={'article': relation(Article, primaryjoin=articles_related_articles.c.article_id==articles.c.id, backref='related'), 'related': relation(Article, primaryjoin=articles_related_articles.c.related_id==articles.c.id, backref='original')}) This would add two properties to your Article class, 'related' and 'original'. Both lists would contain ArticleRelatedArticle instances, which have 'article' , 'related' and 'bias' properties. You might also want to look into the association proxy plugin (http://www.sqlalchemy.org/docs/plugins.html#plugins_associationproxy) which would make this less verbose when you aren't using the 'bias' property. > > I am doing this in the shell, but its not working as expected. I dont > see those two rows created, and related_id is always getting > overrideen to NULL, even though I have it specified while creating the > object. > > In [1]: a = Article.select()[0] > In [3]: a.id > Out[3]: 1L > In [4]: b = Article.select()[1] > In [5]: c = ArticlesRelatedArticles(article_id=a.id, related_id=b.id) > In [6]: a.related.append(c) > In [7]: session.flush() > /usr/lib/python2.4/site-packages/SQLAlchemy-0.3.4-py2.4.egg/sq > lalchemy/ > databases/mysql.py:313: Warning: Field 'related_id' doesn't have a > default value I think this might be because you aren't using assign_mapper for the ArticleRelatedArticle class, so it doesn't get the magic 'assign constructor parameters to attributes' behaviour Hope that helps, Simon --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---