Re: [sqlalchemy] custom __init__ methods not being invoked
Thanks for the details. Makes sense. Still not consistent with what I'm experiencing. Although consistent with what I'm seeing when I put a simple example/test together. I'll keep digging... — RM On Feb 12, 2013, at 4:51 PM, Michael Bayer mike...@zzzcomputing.com wrote: its called in all SQL loading scenarios including that of relationships. A relationship load might not actually result in the object being loaded from the DB in these scenarios: 1. the relationship is a simple many-to-one, and the object could be located by primary key from the identity map without emitting a SQL load. 2. the relationship emitted the SQL, but as it loaded the rows, the objects matching those rows were already in the identity map, so they weren't reconstructed. In both scenarios above, the objects were still guaranteed to be present in the identity map in only three possible ways: 1. they were loaded at some point earlier, in which case your reconstructor was called 2. they moved from pending to persistent , meaning you added them with add(), then they got inserted, so you'd want to make sure whatever regular __init__ does is appropriate here 3. the objects were detached, and were add()ed back into the session, but this still implies that #1 or #2 were true for a previous Session. On Feb 12, 2013, at 5:29 PM, Ryan McKillen ryan.mckil...@gmail.com wrote: It doesn't appear that the method decorated by @orm.reconstructor is called on objects retrieved/loaded as relationships. Not my desired behavior, but I guess it is consistent with the docs: When instances are loaded during a Query operation as in query(MyMappedClass).one(), init_on_load is called. So if I need it to be executed in a relationship-loading situation, what's the best way to go about it? Thanks. — RM On Mon, Jan 7, 2013 at 3:36 AM, Ryan McKillen ryan.mckil...@gmail.com wrote: Worked like a charm. Thanks. — RM On Mon, Jan 7, 2013 at 6:26 PM, Michael van Tellingen michaelvantellin...@gmail.com wrote: See http://docs.sqlalchemy.org/en/latest/orm/mapper_config.html#constructors-and-object-initialization On Mon, Jan 7, 2013 at 4:47 AM, RM ryan.mckil...@gmail.com wrote: I have a class which inherits from Base. My class has a metaclass which inherits from DeclarativeMeta. Among other things, the metaclass adds an __init__ method to the class dictionary. When I instantiate an instance of my class directly, my __init__ method is invoked, but if I use the ORM to retrieve an instance, my __init__ method is not invoked. A metaclass serves better than a mixin for what I am trying to accomplish. However, I did experiment with a mixin and saw the same behavior as described above. Any ideas? Many thanks. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/oDj_bHNvP7EJ. 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. -- 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. -- 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. -- 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. -- 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.
Re: [sqlalchemy] custom __init__ methods not being invoked
It doesn't appear that the method decorated by @orm.reconstructor is called on objects retrieved/loaded as relationships. Not my desired behavior, but I guess it is consistent with the docs: When instances are loaded during a Query operation as in query(MyMappedClass).one(), init_on_load is called. So if I need it to be executed in a relationship-loading situation, what's the best way to go about it? Thanks. — RM On Mon, Jan 7, 2013 at 3:36 AM, Ryan McKillen ryan.mckil...@gmail.comwrote: Worked like a charm. Thanks. — RM On Mon, Jan 7, 2013 at 6:26 PM, Michael van Tellingen michaelvantellin...@gmail.com wrote: See http://docs.sqlalchemy.org/en/latest/orm/mapper_config.html#constructors-and-object-initialization On Mon, Jan 7, 2013 at 4:47 AM, RM ryan.mckil...@gmail.com wrote: I have a class which inherits from Base. My class has a metaclass which inherits from DeclarativeMeta. Among other things, the metaclass adds an __init__ method to the class dictionary. When I instantiate an instance of my class directly, my __init__ method is invoked, but if I use the ORM to retrieve an instance, my __init__ method is not invoked. A metaclass serves better than a mixin for what I am trying to accomplish. However, I did experiment with a mixin and saw the same behavior as described above. Any ideas? Many thanks. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/oDj_bHNvP7EJ. 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. -- 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. -- 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.
Re: [sqlalchemy] custom __init__ methods not being invoked
Worked like a charm. Thanks. — RM On Mon, Jan 7, 2013 at 6:26 PM, Michael van Tellingen michaelvantellin...@gmail.com wrote: See http://docs.sqlalchemy.org/en/latest/orm/mapper_config.html#constructors-and-object-initialization On Mon, Jan 7, 2013 at 4:47 AM, RM ryan.mckil...@gmail.com wrote: I have a class which inherits from Base. My class has a metaclass which inherits from DeclarativeMeta. Among other things, the metaclass adds an __init__ method to the class dictionary. When I instantiate an instance of my class directly, my __init__ method is invoked, but if I use the ORM to retrieve an instance, my __init__ method is not invoked. A metaclass serves better than a mixin for what I am trying to accomplish. However, I did experiment with a mixin and saw the same behavior as described above. Any ideas? Many thanks. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/oDj_bHNvP7EJ. 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. -- 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. -- 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.
Re: [sqlalchemy] Is Session.execute SQL injection safe?
Great. Thank you. On Mon, Feb 28, 2011 at 9:37 PM, Michael Bayer mike...@zzzcomputing.comwrote: On Feb 28, 2011, at 5:35 PM, Ryan wrote: Can't find anything in the docs as to whether query strings passed into Session.execute are escaped/safe from SQL injection. Any insights? Thanks. A literal query string is only safe against injection if you ensure that the string contains no portions of user-entered text inside of it.Bind parameters should always be used for literal values. Docs on the text() construct which Session.execute() uses, and the accepted bind parameter format, are here: http://www.sqlalchemy.org/docs/core/expression_api.html#sqlalchemy.sql.expression.text -- 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. -- 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. -- 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.
Re: [sqlalchemy] self-referential relationship w/ declarative style
I added that in because without it I get: TypeError: Incompatible collection type: User is not list-like On Tue, Feb 22, 2011 at 6:47 PM, Michael Bayer mike...@zzzcomputing.comwrote: On Feb 22, 2011, at 9:03 PM, Ryan McKillen wrote: Mike, thanks a lot. Big help. I'm almost there. This seems to do the trick: usersid = Column(Integer, primary_key=True, key='id') inviter_id = Column(Integer, ForeignKey('users.id')) inviter = relationship('User', uselist = False, backref = backref('invitee', remote_side=usersid, uselist=True), ) When there are two users, one being the inviter (parent) and the other being the invitee (child), it works like a charm: self.assertEqual(invitee1.inviter.id, inviter.id) self.assertEqual(inviter.invitee[0].id, invitee1.id) But add a third user, one being the inviter and two being the invitees, invitee1.inviter is None. probably because of that uselist=False, which makes it into a one-to-one. Adjacency list is a standard single foreign key relationship - one-to-many on one side, many-to-one on the other. There's an illustration of exactly how the data resides in the table: http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships Any ideas for me? On Tue, Feb 22, 2011 at 7:54 AM, Michael Bayer mike...@zzzcomputing.comwrote: On Feb 20, 2011, at 10:12 PM, Ryan wrote: I'm attempting a self-referential mapping on a Client object that includes these two columns: id = Column(Integer, primary_key=True) inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True) Started here with no luck: inviter = relationship('Client', primaryjoin='Client.id == Client.inviter_id', uselist=False) Then read about self-referential mapping in the docs and tried with no luck: inviter = relationship('Client', remote_side='Client.id', uselist=False) And this with an error: relationship('Client', remote_side=[Client.id], uselist=False) Would be a great help to see how this is done in a declarative style. Thanks! the last example in the section http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationshipsillustrates a declarative self-referential relationship. Note that the id Column object can be referenced directly when you're inside the class declaration itself. -- 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. -- 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. -- 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. -- 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. -- 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.
Re: [sqlalchemy] self-referential relationship w/ declarative style
Got it. Many thanks! On Tue, Feb 22, 2011 at 7:02 PM, Michael Bayer mike...@zzzcomputing.comwrote: one side scalar, one side collection. the collection side you use .append(). You decide which end is the non-collection by setting remote_side, in your code below its invitee. On Feb 22, 2011, at 9:59 PM, Ryan McKillen wrote: I added that in because without it I get: TypeError: Incompatible collection type: User is not list-like On Tue, Feb 22, 2011 at 6:47 PM, Michael Bayer mike...@zzzcomputing.comwrote: On Feb 22, 2011, at 9:03 PM, Ryan McKillen wrote: Mike, thanks a lot. Big help. I'm almost there. This seems to do the trick: usersid = Column(Integer, primary_key=True, key='id') inviter_id = Column(Integer, ForeignKey('users.id')) inviter = relationship('User', uselist = False, backref = backref('invitee', remote_side=usersid, uselist=True), ) When there are two users, one being the inviter (parent) and the other being the invitee (child), it works like a charm: self.assertEqual(invitee1.inviter.id, inviter.id) self.assertEqual(inviter.invitee[0].id, invitee1.id) But add a third user, one being the inviter and two being the invitees, invitee1.inviter is None. probably because of that uselist=False, which makes it into a one-to-one. Adjacency list is a standard single foreign key relationship - one-to-many on one side, many-to-one on the other. There's an illustration of exactly how the data resides in the table: http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships Any ideas for me? On Tue, Feb 22, 2011 at 7:54 AM, Michael Bayer mike...@zzzcomputing.comwrote: On Feb 20, 2011, at 10:12 PM, Ryan wrote: I'm attempting a self-referential mapping on a Client object that includes these two columns: id = Column(Integer, primary_key=True) inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True) Started here with no luck: inviter = relationship('Client', primaryjoin='Client.id == Client.inviter_id', uselist=False) Then read about self-referential mapping in the docs and tried with no luck: inviter = relationship('Client', remote_side='Client.id', uselist=False) And this with an error: relationship('Client', remote_side=[Client.id], uselist=False) Would be a great help to see how this is done in a declarative style. Thanks! the last example in the section http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationshipsillustrates a declarative self-referential relationship. Note that the id Column object can be referenced directly when you're inside the class declaration itself. -- 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. -- 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. -- 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. -- 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. -- 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. -- 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. -- 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