lets all repeat the mantra...."association tables with any columns  
beyond the two foreign keys use the association object pattern".  I  
guess its a little too wordy to be catchy.   Documented at 
http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_relation_patterns_association
 
  .




On May 12, 2008, at 10:45 AM, [EMAIL PROTECTED] wrote:

>
> Hello All,
> I am trying to understand how to use SA and need some help.
>
> I have several tables with 2 many-to-many relations with extra columns
> and 1 only with foreign keys.
> See below for the definitions of tables and mappers. I also created
> classes for all tables (entities and associations).
>
> 1) For the association without extra-column (self.correspond), no
> problem.
> I can add questions and categories. For instance:
> q=Question(question='blabla')
> c=Category('cat1')
> q.categories.append(c)
> session.save(q)
> session.commit()
>
> 2) For the 2 other which have extra-columns, I don't understand how to
> manage.
> For info, these 2 associations relate to both the users and the
> questions tables.
> For instance, how can I add a question related to a user, ie go
> through the ask relation ?
> I went through the excellent documentation but I have to admit that I
> don't understand...
>
> Can somebody :
> - check my mappers are well defined (those with extra columns:
> askMapper and answerMapper and also questMapper)
> - briefly explain me how to handle operations between users and
> questions tables through these mappers
> I'm hoping it is clear enough
>
> Thanks a lot in advance for your help
> Dominique
>
>
> Tables and relations are as follows:
> #Entities
> self.users = Table('users',self.metadata,
>                    Column('user_id', Integer, primary_key = True),
>                    Column('user_name', Unicode(25), unique = True))
>
> self.categories = Table('categories',self.metadata,
>                    Column('categ_id',Integer, primary_key = True),
>                    Column('categ_name',Unicode(250), unique = True))#
> rajouter unique
>
> self.questions = Table('questions', self.metadata,
>                    Column('quest_id', Integer, primary_key = True),
>                    Column('question', Unicode(300)))
>
> # Associations
> self.correspond = Table('categories_questions', self.metadata,
>                    Column('quest_id', Integer,
> ForeignKey('questions.quest_id'), primary_key = True),
>                    Column('categ_id', Integer,
> ForeignKey('categories.categ_id'), primary_key = True))
>
> self.ask = Table('ask', self.metadata,
>                    Column('user_id',Integer,
> ForeignKey('users.user_id'), primary_key = True),
>                    Column('quest_id',Integer,
> ForeignKey('questions.quest_id'), primary_key = True),
>                    Column('data1',Integer, nullable = False, default
> = 50))
>
> self.answer = Table('answer',self.metadata,
>                    Column('user_id',Integer,
> ForeignKey('users.user_id'), primary_key=True),
>                    Column('quest_id',Integer,
> ForeignKey('questions.quest_id'), primary_key=True),
>                    Column('data2',Integer),
>                    ForeignKeyConstraint(['user_id','quest_id'],
> ['ask.user_id','ask.quest_id']))
>
> # mappers
> self.userMapper = mapper(User, self.users)
> self.categMapper = mapper(Category, self.categories)
>
> self.questMapper = mapper(Question, self.questions, properties ={
>                    # ManyToMany CorrespondAssociation between
> questions and categories
>                    'categories': relation(Category, secondary =
> self.correspond, backref='questions'),
>
>                    # ManyToMany AskAssociation between questions and
> users
>                    'users': relation(AskAss, backref='questions'),
>
>                    # ManyToMany AnswerAssociation between questions
> and users
>                    'users': relation(AnswerAss, backref='questions')
>                    })
>
> self.askMapper = mapper(AskAss, self.poser, properties = {
>                    # Ask Association between questions and users
>                    'users': relation(User, backref = 'ask')
>                    })
>
> self.answerMapper = mapper(AnswerAss, self.answer, properties = {
>                    # ManyToMany AnswerAssociation between questions
> and users
>                    'users': relation(User, backref = 'answer')
>                    })
>
> >


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to