[sqlalchemy] Re: Weird error when adding a new object
Got it. I just change session.add(screenGroup) to session.merge(screenGroup) and it works. Thank you for you help, it's always really useful. On Nov 19, 3:10 pm, Michael Bayer wrote: > The most suspicious thing is self.__dict__['foo'] = list() as well as the > direct access to self.__dict__['foo']. If "self" is a mapped object, then > thats your issue. > > Also note this is still an out of context cut-and-paste, if you really want > me to figure it out it has to be an isolated test script. > > On Nov 19, 2010, at 2:49 PM, Alvaro Reinoso wrote: > > > Ok, This is the code: > > > �...@staticmethod > > def insert(string, mode): > > """Insert a new item given a string""" > > screenGroup = ScreenGroup() > > session = rdb.Session() > > > try: > > if mode == "XML": > > screenGroup.fromXML(string) > > elif mode == "JSON": > > screenGroup.fromJSON(string) > > if screenGroup.id == None: > > session.add(screenGroup) > > else: > > screenGroup = session.merge(screenGroup) > > > session.flush() > > result = "Successful:" + str(screenGroup.id) + "," + > > screenGroup.title > > return result > > except Exception, e: > > log.warn("::ScreenGroupManager, insert > The error is " > > + str(e)) > > return "Error connecting to the database. Please > > try again > > later." > > > return "Error connecting to the database. Please try again > > later." > > > This is the function where it gets the error. When I already have the > > item and I merge It works out. However, if it's a new item, breaks. > > This anothe fromXML function: > > > def fromXML(self, data): > > """Unserializes this class from an XML formatted file or > > string.""" > > root = "" > > try: > > if(data.endswith(".xml")): > > doc = etree.parse(data) > > root = doc.getroot() > > else: > > root = etree.XML(data) > > except etree.XMLSyntaxError, e: > > print e > > pass > > > if not type(root) == type(etree.XML("test")): > > return > > > if root.tag == "screenGroup": > > for child in root.iterchildren(): > > if child.tag == "screens" or child.tag == > > "screenGroups": > > if child.text: > > items = child.text.split(",") > > self.__dict__[child.tag] = list() > > for itemId in items: > > if child.tag == > > "screens": > > item = > > ScreenManager.getScreenById(itemId) > > else: > > item = > > ScreenGroupManager.getGroupScreenById(itemId) > > > > self.__dict__[child.tag].append(item) > > else: > > self.setAttr(child.tag, child.text) > > > ScreenManager.getScreenById(itemId) and > > ScreenGroupManager.getGroupScreenById(itemId) just get the item from > > the database (session.query(Screen).get(int(itemId))). > > > This is the XML string : Group 10 > title>4,3 > screenGroup> > > > Thanks! > > > On Nov 18, 7:07 pm, Michael Bayer wrote: > >> looks fine to me, would need full stack trace + single script file of > >> runnable, reproducing code, thanks. > > >> On Nov 18, 2010, at 6:37 PM, Alvaro Reinoso wrote: > > >>> Hello, > > >>> When trying to add a new item doing: > > >>> session = session.add(mediaGroup) > > >>> I got this error: > > >>> Module sqlalchemy.orm.attributes:834 in get_collection > >>> return getattr(user_data, '_sa_adapter') > >>> class GenericBackrefExtension(interfaces.AttributeExtension):>> > >>> return getattr(user_data, '_sa_adapter') > > >>> "AttributeError: 'list' object has no attribute '_sa_adapter'" > > >>> This object's class: > > >>> class ScreenGroup(rdb.Model): > >>> """The ScreenGroup is a class derived from ScreenGroup, it is used to > >>> control users within a group""" > >>> rdb.metadata(metadata) > >>> rdb.tablename("screen_groups") > > >>> id = Column("id", Integer, primary_key=True) > >>> title = Column("title", String(100)) > >>> parents = Column("parents", String(512)) > > >>> screens = relationship("Screen", secondary=group_screens, > >>> order_by="Screen.title", backref="screen_g
Re: [sqlalchemy] Re: Weird error when adding a new object
The most suspicious thing is self.__dict__['foo'] = list() as well as the direct access to self.__dict__['foo']. If "self" is a mapped object, then thats your issue. Also note this is still an out of context cut-and-paste, if you really want me to figure it out it has to be an isolated test script. On Nov 19, 2010, at 2:49 PM, Alvaro Reinoso wrote: > Ok, This is the code: > > @staticmethod > def insert(string, mode): > """Insert a new item given a string""" > screenGroup = ScreenGroup() > session = rdb.Session() > > try: > if mode == "XML": > screenGroup.fromXML(string) > elif mode == "JSON": > screenGroup.fromJSON(string) > if screenGroup.id == None: > session.add(screenGroup) > else: > screenGroup = session.merge(screenGroup) > > session.flush() > result = "Successful:" + str(screenGroup.id) + "," + > screenGroup.title > return result > except Exception, e: > log.warn("::ScreenGroupManager, insert > The error is " > + str(e)) > return "Error connecting to the database. Please > try again > later." > > return "Error connecting to the database. Please try again > later." > > This is the function where it gets the error. When I already have the > item and I merge It works out. However, if it's a new item, breaks. > This anothe fromXML function: > > def fromXML(self, data): > """Unserializes this class from an XML formatted file or > string.""" > root = "" > try: > if(data.endswith(".xml")): > doc = etree.parse(data) > root = doc.getroot() > else: > root = etree.XML(data) > except etree.XMLSyntaxError, e: > print e > pass > > if not type(root) == type(etree.XML("test")): > return > > if root.tag == "screenGroup": > for child in root.iterchildren(): > if child.tag == "screens" or child.tag == > "screenGroups": > if child.text: > items = child.text.split(",") > self.__dict__[child.tag] = > list() > for itemId in items: > if child.tag == > "screens": > item = > ScreenManager.getScreenById(itemId) > else: > item = > ScreenGroupManager.getGroupScreenById(itemId) > > self.__dict__[child.tag].append(item) > else: > self.setAttr(child.tag, child.text) > > ScreenManager.getScreenById(itemId) and > ScreenGroupManager.getGroupScreenById(itemId) just get the item from > the database (session.query(Screen).get(int(itemId))). > > This is the XML string : Group 10 title>4,3 screenGroup> > > Thanks! > > On Nov 18, 7:07 pm, Michael Bayer wrote: >> looks fine to me, would need full stack trace + single script file of >> runnable, reproducing code, thanks. >> >> On Nov 18, 2010, at 6:37 PM, Alvaro Reinoso wrote: >> >>> Hello, >> >>> When trying to add a new item doing: >> >>> session = session.add(mediaGroup) >> >>> I got this error: >> >>> Module sqlalchemy.orm.attributes:834 in get_collection >>> return getattr(user_data, '_sa_adapter') >>> class GenericBackrefExtension(interfaces.AttributeExtension):>> >>> return getattr(user_data, '_sa_adapter') >> >>> "AttributeError: 'list' object has no attribute '_sa_adapter'" >> >>> This object's class: >> >>> class ScreenGroup(rdb.Model): >>>"""The ScreenGroup is a class derived from ScreenGroup, it is used to >>> control users within a group""" >>>rdb.metadata(metadata) >>>rdb.tablename("screen_groups") >> >>>id = Column("id", Integer, primary_key=True) >>>title = Column("title", String(100)) >>>parents = Column("parents", String(512)) >> >>>screens = relationship("Screen", secondary=group_screens, >>> order_by="Screen.title", backref="screen_groups") >>>screenGroups = relationship("ScreenGroup", >>> secondary=screen_group_groups, order_by="ScreenGroup.title", >>>primaryjoin=lambda: ScreenGroup.id == >>> screen_group_groups.c.screen_
[sqlalchemy] Re: Weird error when adding a new object
Ok, This is the code: @staticmethod def insert(string, mode): """Insert a new item given a string""" screenGroup = ScreenGroup() session = rdb.Session() try: if mode == "XML": screenGroup.fromXML(string) elif mode == "JSON": screenGroup.fromJSON(string) if screenGroup.id == None: session.add(screenGroup) else: screenGroup = session.merge(screenGroup) session.flush() result = "Successful:" + str(screenGroup.id) + "," + screenGroup.title return result except Exception, e: log.warn("::ScreenGroupManager, insert > The error is " + str(e)) return "Error connecting to the database. Please try again later." return "Error connecting to the database. Please try again later." This is the function where it gets the error. When I already have the item and I merge It works out. However, if it's a new item, breaks. This anothe fromXML function: def fromXML(self, data): """Unserializes this class from an XML formatted file or string.""" root = "" try: if(data.endswith(".xml")): doc = etree.parse(data) root = doc.getroot() else: root = etree.XML(data) except etree.XMLSyntaxError, e: print e pass if not type(root) == type(etree.XML("test")): return if root.tag == "screenGroup": for child in root.iterchildren(): if child.tag == "screens" or child.tag == "screenGroups": if child.text: items = child.text.split(",") self.__dict__[child.tag] = list() for itemId in items: if child.tag == "screens": item = ScreenManager.getScreenById(itemId) else: item = ScreenGroupManager.getGroupScreenById(itemId) self.__dict__[child.tag].append(item) else: self.setAttr(child.tag, child.text) ScreenManager.getScreenById(itemId) and ScreenGroupManager.getGroupScreenById(itemId) just get the item from the database (session.query(Screen).get(int(itemId))). This is the XML string : Group 104,3 Thanks! On Nov 18, 7:07 pm, Michael Bayer wrote: > looks fine to me, would need full stack trace + single script file of > runnable, reproducing code, thanks. > > On Nov 18, 2010, at 6:37 PM, Alvaro Reinoso wrote: > > > Hello, > > > When trying to add a new item doing: > > > session = session.add(mediaGroup) > > > I got this error: > > > Module sqlalchemy.orm.attributes:834 in get_collection > > return getattr(user_data, '_sa_adapter') > > class GenericBackrefExtension(interfaces.AttributeExtension):>> > > return getattr(user_data, '_sa_adapter') > > > "AttributeError: 'list' object has no attribute '_sa_adapter'" > > > This object's class: > > > class ScreenGroup(rdb.Model): > > """The ScreenGroup is a class derived from ScreenGroup, it is used to > > control users within a group""" > > rdb.metadata(metadata) > > rdb.tablename("screen_groups") > > > id = Column("id", Integer, primary_key=True) > > title = Column("title", String(100)) > > parents = Column("parents", String(512)) > > > screens = relationship("Screen", secondary=group_screens, > > order_by="Screen.title", backref="screen_groups") > > screenGroups = relationship("ScreenGroup", > > secondary=screen_group_groups, order_by="ScreenGroup.title", > > primaryjoin=lambda: ScreenGroup.id == > > screen_group_groups.c.screen_groupA_id, > > secondaryjoin=lambda: ScreenGroup.id == > > screen_group_groups.c.screen_groupB_id, > > backref="screen_groups") > > > Thanks in advance! > > > -- > > You received this message because you are subscribed to the Google Groups > > "sqlalchemy" group. > > To post to this group, send email to sqlalch...@googlegroups.com. > > To unsubscribe from this group, send email to > > sqlalchemy+unsubscr...@googlegroups.com. > > For more option