python - Removing/Adding an Object from a Collection in SQLAlchemy -
issue @ hand: removing user group , adding user group
many-to-one table relationship
(many users 1 group)
using: sqlalchemy 1.0.9, python 2.7.9
db operates off of crud
operations:
1. create group (e.g. 'admin')
2. retrieve group (using either group name or id)
3. update:
a) update : add_user group ('joe' 'admin')
b) update : remove_user group ('bob' 'editor')
4. delete group
i newbie trying hands dirty, smartly here. 1. attempting remove .user
group()
in function: update_remove_user
without deleting user
user()
. found similar want on stacks here. , in sqlalchemy docs
2. adding user existing group. right now, function creating group , user instead of retrieving group , adding user it.
model: base.py
class group(base): __tablename__ = 'groups' id = column(integer, primary_key=true) group_name = column(string(50), unique=true) created_on = column(datetime, default=datetime.utcnow) modified_on = column(datetime, onupdate=datetime.utcnow) #new user_id = column(integer, foreignkey('users.id')) user = relationship('user', backref='groups') def __init__(self, group_name, *args): self.group_name = group_name self.args = args def __repr__(self): return "<group(group_name='%s')>" % (self.group_name)
object.py
def create_group(self, group_name): new_group = group(group_name) self.session.add(new_group) self.session.commit() return new_group print 'this new group: ', new_group def update_group_add_user(self, group_name, user): g = self.session.query(group).\ filter(group.group_name == group_name).one() if g: g.append(user) # append not found self.session.commit() def update_group_remove_user(self, group_name, user): remove_user = self.session.query(group).\ filter(group.group_name == group_name).\ filter(group.user == user).one() if remove_user: r = remove_user.user print 'test r : ', r g = group() # not work g.remove(r) self.session.commit()
changes per suggestions below:
works except add_user
replaces user
new user
when added same group (as indicated alex) many-to-one relationship. added many-to-many relationship , same issue occurring. user being replace in many-to-many relationship.
changes reflected # below of working code has resolved issues.code working n-n relationship.
object.py
def update_group_add_user(self, group_name, user): g = self.session.query(group).\ filter(group.group_name == group_name).one() if g: found_name = g.group_name g.users = [user] # < replaced working: g.users.append(user) print 'found_name , user: ', found_name, user self.session.commit() def update_group_remove_user(self, group_name, user): remove_user = self.session.query(group).\ filter(group.group_name == group_name).one() if remove_user: remove_user.users = none # < replaced working remove_user.users.remove(user) self.session.commit()
new many many design:
association_t = table('group_user_link', base.metadata, column('group_id', integer, foreignkey('groups.id')), column('user_id', integer, foreignkey('users.id'))) class group(base): __tablename__ = 'groups' id = column(integer, primary_key=true) name = column(string(50), unique=true) user = relationship('user', secondary='group_user_link', backref='groups') # more... class user(base): __tablename__ = 'users' id = column(integer, primary_key=true) username = column(string(40), unique=true) # more info
first of all, sure want create many-to-one relationship?
according relationship 1 "group" can contain 1 user. opposite term "group". think, should create many-to-many relationship.
as first issue:
g = self.session.query(group).\ filter(group.group_name == group_name).one() ... g.append(user) # append not found
when make such query, result instance of group
class. , instance doesn't have append
method. right way change user group is:
g.user = user
as second issue:
def update_group_remove_user(self, group_name, user): remove_user = self.session.query(group).\ filter(group.group_name == group_name).\ filter(group.user == user).one() if remove_user: r = remove_user.user print 'test r : ', r g = group() # not work g.remove(r) self.session.commit()
again, remove_user
instance of group
(so, have chosen wrong name it).
when tried g = group(); g.remove(r)
created new instance of group
, tried call method doesn't exists. so, in order delete user group should:
remove_user.user = none self.session.commit()
Comments
Post a Comment