-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

--- Begin Message ---
Hi all,

I need urgent help on SQLAlchemy relations. I have three classes User,
Group, Project and association user_group_table.



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)).encode('utf-8')

    def __unicode__(self):
        return self.user_name



class Group(DeclarativeBase):
    __tablename__ = 't_group'
    group_id = Column(Integer, autoincrement=True, primary_key=True)
    group_name = Column(Unicode(16), unique=True)
    
    users=relation('User', secondary=user_group_table,backref='groups')
    
    def __repr__(self):
        return ('<Group: name=%s>' % self.group_name).encode('utf-8')

    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)
    
    project=relation('Group', secondary=auth.user_group_table,
backref='Project')

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


#association table
user_group_table = Table('t_user_group', metadata,
    Column('user_id', Integer, ForeignKey('t_user.user_id',
        onupdate="CASCADE", ondelete="CASCADE")),
    Column('group_id', Integer, ForeignKey('t_group.group_id',
        onupdate="CASCADE", ondelete="CASCADE")),
   Column('project_id', Integer, ForeignKey('t_project.project_id',
        onupdate="CASCADE", ondelete="CASCADE"))
)
                                        

I am unable to insert the records into association table....
below is the code snippet for insertion

user = DBSession.query(User).filter(User.user_name == kw['PM']).one()
group = DBSession.query(Group).filter(Group.group_name == 'pm').one()
project = DBSession.query(Project).\            filter(Project.project_id
== kw['project_id']).one()
        
        
                group.users.append(user)                
                project.project.append(group)
                DBSession.flush()
                transaction.commit()


Please help me ASAP.


Thanx in advance

--- End Message ---

Reply via email to