Am 17.02.2011 13:00 schrieb Abdul Gaffar:
I tried this example, I am facing proble while inserting the records..

I think the problem is you can't easily associate items with SQLAlchemy by using the relationship property. It's really made only for binary relationships. I guess you need to do it explicitely, like this:

user = User.query.filter_by(user_name='abdul').one()
group = Group.query.filter_by(group_name='foo').one()
project = Project.query.filter_by(project_name='tg').one()

DBSession.add(Association(user, group, project))

DBSession.flush()

print user.groups_for_project(project)

where the classes are defined like that:


class Association(DeclarativeBase):
    __tablename__ = 't_assoc'

    user_id = Column(Integer, ForeignKey('t_user.user_id',
        onupdate="CASCADE", ondelete="CASCADE"), primary_key=True)
    group_id = Column(Integer, ForeignKey('t_group.group_id',
        onupdate="CASCADE", ondelete="CASCADE"),  primary_key=True,)
    project_id = Column(Integer, ForeignKey('t_project.project_id',
        onupdate="CASCADE", ondelete="CASCADE"), primary_key=True)

    user = relationship('User', backref='associations',
        cascade="all, delete")
    group = relationship('Group', backref='associations',
        cascade="all, delete")
    project = relationship('Project', backref='associations',
        cascade="all, delete")

    def __init__(self, user, group, project):
        self.user_id = user.user_id
        self.group_id = group.group_id
        self.project_id = project.project_id


class User(DeclarativeBase):
    __tablename__ = 't_user'

    user_id = Column(Integer, autoincrement=True, primary_key=True)
    user_name = Column(Unicode(32), unique=True, nullable=False)
    email_address = Column(Unicode(320), unique=True, nullable=False,
                           info={'rum': {'field':'Email'}})

    def __repr__(self):
        return ('<User: user_name=%r, email=%r>' % (
                self.user_name, self.email_address))

    def __unicode__(self):
        return self.user_name

    def groups_for_project(self, project):
        return Project.query.join(Association).filter_by(
            project_id=project.project_id, user_id=self.user_id).all()


class Group(DeclarativeBase):
    __tablename__ = 't_group'

    group_id = Column(Integer, autoincrement=True, primary_key=True)
    group_name = Column(Unicode(16), unique=True)

    def __repr__(self):
        return '<Group: name=%r>' % self.group_name

    def __unicode__(self):
        return self.group_name


class Project(DeclarativeBase):
    __tablename__ = 't_project'

    project_id = Column(Integer, autoincrement=True, primary_key=True)
    project_name = Column(Unicode(80), unique=True, nullable=False)

    def __repr__(self):
        return '<Project(%r)>' % self.project_name


Similarly, you can define User.projects_for_group and the corresponding methods on the other classes.

But you'll probably get a more profound answer on the SA mailing list (if you follow http://www.gerv.net/hacking/how-to-ask-good-questions/).

-- Christoph

--
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en.

Reply via email to