I have a User model, and a Group model, and a m2m relation between them.

I also have a proxy on the user that routes directly to the Group 
instances. The creator function, however, only takes one param (group), so 
when I pass in a group, I have no way to look it up to see if the (user_id, 
group_id) combo is in use already, because i only have the group. It seems 
like I am probably missing something very simple, but I can't figure out 
what it is. The code below can be run to show the issue I am having.

The last line, user.groups = [group] , throws an error because 'self' isn't 
defined. self clearly doesn't work here, what would the workaround be to 
get the User instance's "id" value into that creator function? The group I 
am passing in is the same as the group currently assigned, so rather than 
purging the collection and adding a UserGroup with only a group_id (the 
user_id gets populated later via the relationship, but at that point it is 
too late, because it's already decided the new object is not the same as 
the old, and it purges the old one), i'd like to be able to identify that 
the identity_key is the same, and just let it be.

####Code below######

from sqlalchemy import *
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship, backref


e = create_engine('sqlite:////tmp/test.db', echo=False)
Base = declarative_base()
Base.metadata = MetaData(e)


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)

    def usergroup_creator(group):
        session = Session(e)
        ugroup = session.query(UserGroup) \
            .filter_by(user_id=self.id) \
            .filter_by(group_id=group.id) \
            .first()
        if not ugroup:
            return UserGroup(group=group)

    groups = association_proxy('user_groups', 'group',
        creator=usergroup_creator)

class Group(Base):
    __tablename__ = 'groups'
    id = Column(Integer, primary_key=True)
    
class UserGroup(Base):
    __tablename__ = 'user_groups'
    user_id = Column(Integer, ForeignKey(User.id), primary_key=True)
    group_id = Column(Integer, ForeignKey(Group.id), primary_key=True)

    user = relationship(User, backref='user_groups')
    group = relationship(Group, backref='user_groups')
    
if __name__ == '__main__':
    Base.metadata.drop_all()
    Base.metadata.create_all()
    
    user = User(id=1)
    group = Group(id=1)
    ugroup = UserGroup(user=user, group=group)

    session = Session(e)
    session.add(user)
    session.add(group)
    session.add(ugroup)
    session.commit()
    session.expunge_all()
    
    user = session.query(User).get(1)
    group = session.query(Group).get(1)
    user.groups = [group]

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to