[sqlalchemy] Re: Multiple database connections - Using ORM to copy table data from one DB to another
I had originally tried the expunge method, as in the code I had attached, but if I just use session.save(policy), I get the aforementioned exception, and if I use session.save_or_update(policy) it simply does nothing. When using session.merge(policy), I get the following exception: File build\bdist.win32\egg\sqlalchemy\orm\session.py, line 483, in merge NameError: global name 'mapperutil' is not defined Regarding the eager-loading, I understand that the configuration of the mappers will be in effect. I meant to inquire as to whether there was a way to recursively override the mappers, as this is a different use than the ORM was originally intended for, and the usual behavior of lazy-loading is not desired here. I considered using the eagerload option, but my ORM has several levels (eg, Policy contains Insureds which each contain Addresses) and it didn't seem like the option would apply all the way down. -Original Message- From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED] Behalf Of Michael Bayer Sent: Saturday, September 22, 2007 11:11 AM To: sqlalchemy@googlegroups.com Subject: [sqlalchemy] Re: Multiple database connections - Using ORM to copy table data from one DB to another On Sep 18, 2007, at 5:47 PM, Cory Johns wrote: I'm trying to make a small utility that uses a larger application's ORM to copy an object from one database (dev) to another (qa) for testing purposes. But I'm running in to trouble getting SQLAlchemy to use the multiple database connections. I can get the object to load, and then open a connection to the other database, but when I try to call save, I get the following error: sqlalchemy.exceptions.InvalidRequestError: Instance 'thig.base.model.policy.Policy object at 0x018C5AB0' is a detached instance or is already persistent in a different Session Is it possible to re-attach an ORM instance to a new session in order to duplicate the data to another database like I'm doing? If so, how do I go about that? use session.merge() is probably the most straightforward way (returns a second object instance associated with the new session). or, you can expunge() the object from the first session, then save_or_update () to the second session. Additionally, as I suspect this will be a problem once I get it re- attached, is there an easy way to specify that all properties, recursively, should be eager loaded? whatever the configuration is of the mappers() representing the involved classes will remain in effect. CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Problem when applying max_identifier_length
Since upgrading to 0.3.8 (from 0.3.6), I've run in to a problem with long identifiers. It seems that _truncate_bindparam is being applied to both parts of the ClauseElements (that is, the parts both before and after the AS), and is testing = (instead of ) max_identifier_length, so that when I have a column identifier that is the maximum length, it ends up creating a no such column error. An example test-case follows: from sqlalchemy import * from sqlalchemy.engine.url import URL class Underwriter(object): def __init__(self, source, explan): self.primaryHeatSource = source self.primaryHeatSourceExplan = explan def init_database(db): metadata = BoundMetaData(db) underwriting = Table('und_underwriting_tbl', metadata, Column('und_underwriting_id', Integer, primary_key=True, key='id'), Column('und_primary_heat_source', String(20), key='primaryHeatSource'), Column('und_primary_heat_source_explan', String(100), key='primaryHeatSourceExplan'), ) mapper(Underwriter, underwriting) # Add a test record to the DB session = create_session() underwriting.create() uw = Underwriter('bonfire', 'Maybe she lives in the woods?') session.save(uw) session.flush() session.close() db = create_engine(URL(drivername='sqlite', database=':memory:')) # simulate running under Oracle db.dialect.max_identifier_length = lambda: 30 db.echo = True # insert for test record seems to work OK init_database(db) session = create_session() # select, however, fails query = session.query(Underwriter) underwriters = query.select() session.close() Throws the following error (note the und_underwriting_tbl.und_primary_heat_source__1 AS und_underwriting_tbl_und_2): ... File build\bdist.win32\egg\sqlalchemy\engine\base.py, line 602, in _execute sqlalchemy.exceptions.SQLError: (OperationalError) no such column: und_underwriting_tbl.und_primary_heat_source__1 u'SELECT und_underwriting_tbl.und_primary_heat_source__1 AS und_underwriting_tbl_und_2, und_underwriting_tbl.und_underwriting_id AS und_underwriting_tbl_und_3, und_underwriting_tbl.und_primary_heat_source AS und_underwriting_tbl_und_4 \nFROM und_underwriting_tbl ORDER BY und_underwriting_tbl.oid' [] Thank you, Cory Johns Systems Tower Hill Insurance Group, Inc. CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Cached ORM instances and eagerload queries
I find myself in a situation where I need certain (normally lazy-loaded) properties of an ORM class to be eagerloaded for a particular query. I pass withoptions=[eagerload('property')] in to session.query(), and everything works fine. At least, it did until the query picked up a record that had been previously returned by another query without eagerloading. Because the record was already cached in the session, it didn't pick up on the eagerload option. What I'd like to know is, is there a way to force the eagerloading query to apply its eagerloading behavior, even if the record is already cached in the session? Thanks, Cory Johns Systems Tower Hill Insurance Group, Inc. CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Cached ORM instances and eagerload queries
Excellent, thanks. Works a treat. -Original Message- From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED] Behalf Of Michael Bayer Sent: Wednesday, June 13, 2007 4:51 PM To: sqlalchemy Subject: [sqlalchemy] Re: Cached ORM instances and eagerload queries ... query= session.query(MyClass).options(..).filter_by(..)..etc.. result = query._select_statement(query.compile(), populate_existing=True) ... CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Determining class of ORM attribute at run-time
I'm creating a general routine that is intended to take an arbitrary dict of attribute / value pairs and an ORM object instance, and use the dict to update the properties of the ORM instance, without knowing a priori whether the attributes might be relations to other tables. The problem I ran in to is when a property that is a relation needs to be created anew, because then I don't know what class to create an instance of. From looking at the source, I found what I was looking for under the argument attribute of the PropertyLoader class, so I am currently doing along the lines of: attr_class = object_mapper(ormvar).properties[attr].argument new_instance = attr_class() setattr(ormvar, attr, new_instance) However, this feels like it might be a bit fragile, as the argument attribute isn't explicitly document that I could find. Is there an API / documented way of determining the class that represents a particular attribute, as determined by mapper() or relation()? Hope this makes sense. Thanks. - Cory Johns CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---