Re: [sqlalchemy] orm object, before after

2010-10-16 Thread Michael Hipp

On 8/24/2010 9:47 PM, Michael Bayer wrote:

Michael Hipp wrote:

How do I make a copy of an orm object such that modifications to the
copy do not affect the original?


The mapped object has a member _sa_instance_state that you basically
don't want to transfer to your new object.You want it to have its own
_sa_instance_state and this comes from calling the plain constructor,
which the copy module, if that's what you're using, does not use.   You
also want to set attributes normally, not populating __dict__ directly.
So just basically don't use the copy module.

 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))


Resurrecting an old thread ...

I'm just now getting around to try this but I'm finding out it doesn't really 
work like I'd hoped.


As soon as 'getattr' hits a column with a ForeignKey it immediately tries to 
autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be saved.


In fact, I don't ever want to save 'x', and I especially don't want to INSERT 
it. It would be a duplicate of 'myoldobject'.


Is there a way to copy an orm object and tell it don't ever save this I just 
want to keep it around to look at?


Alternatively I can just copy all the attributes to a dict(), but that's a bit 
messy.


Thanks for all your help,
Michael

--
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] orm object, before after

2010-10-16 Thread Michael Bayer

On Oct 16, 2010, at 1:02 PM, Michael Hipp wrote:

 On 8/24/2010 9:47 PM, Michael Bayer wrote:
 Michael Hipp wrote:
 How do I make a copy of an orm object such that modifications to the
 copy do not affect the original?
 
 The mapped object has a member _sa_instance_state that you basically
 don't want to transfer to your new object.You want it to have its own
 _sa_instance_state and this comes from calling the plain constructor,
 which the copy module, if that's what you're using, does not use.   You
 also want to set attributes normally, not populating __dict__ directly.
 So just basically don't use the copy module.
 
 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))
 
 Resurrecting an old thread ...
 
 I'm just now getting around to try this but I'm finding out it doesn't really 
 work like I'd hoped.
 
 As soon as 'getattr' hits a column with a ForeignKey it immediately tries to 
 autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be saved.
 
 In fact, I don't ever want to save 'x', and I especially don't want to INSERT 
 it. It would be a duplicate of 'myoldobject'.
 
 Is there a way to copy an orm object and tell it don't ever save this I just 
 want to keep it around to look at?
 
 Alternatively I can just copy all the attributes to a dict(), but that's a 
 bit messy.

dont put it in the Session.




 
 Thanks for all your help,
 Michael
 
 -- 
 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 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 sqlalch...@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] orm object, before after

2010-10-16 Thread Michael Hipp

On 10/16/2010 12:52 PM, Michael Bayer wrote:


On Oct 16, 2010, at 1:02 PM, Michael Hipp wrote:


On 8/24/2010 9:47 PM, Michael Bayer wrote:

Michael Hipp wrote:

How do I make a copy of an orm object such that modifications to the
copy do not affect the original?


The mapped object has a member _sa_instance_state that you basically
don't want to transfer to your new object.You want it to have its own
_sa_instance_state and this comes from calling the plain constructor,
which the copy module, if that's what you're using, does not use.   You
also want to set attributes normally, not populating __dict__ directly.
So just basically don't use the copy module.

 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))


Resurrecting an old thread ...

I'm just now getting around to try this but I'm finding out it doesn't really 
work like I'd hoped.

As soon as 'getattr' hits a column with a ForeignKey it immediately tries to 
autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be saved.

In fact, I don't ever want to save 'x', and I especially don't want to INSERT 
it. It would be a duplicate of 'myoldobject'.

Is there a way to copy an orm object and tell it don't ever save this I just want 
to keep it around to look at?

Alternatively I can just copy all the attributes to a dict(), but that's a bit 
messy.


dont put it in the Session.


That makes sense. But how do I not do that? As in your example code above I'm 
not adding it to a session, at least not intentionally.


Thanks,
Michael

--
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] orm object, before after

2010-10-16 Thread Michael Bayer

On Oct 16, 2010, at 2:03 PM, Michael Hipp wrote:

 On 10/16/2010 12:52 PM, Michael Bayer wrote:
 
 On Oct 16, 2010, at 1:02 PM, Michael Hipp wrote:
 
 On 8/24/2010 9:47 PM, Michael Bayer wrote:
 Michael Hipp wrote:
 How do I make a copy of an orm object such that modifications to the
 copy do not affect the original?
 
 The mapped object has a member _sa_instance_state that you basically
 don't want to transfer to your new object.You want it to have its own
 _sa_instance_state and this comes from calling the plain constructor,
 which the copy module, if that's what you're using, does not use.   You
 also want to set attributes normally, not populating __dict__ directly.
 So just basically don't use the copy module.
 
 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))
 
 Resurrecting an old thread ...
 
 I'm just now getting around to try this but I'm finding out it doesn't 
 really work like I'd hoped.
 
 As soon as 'getattr' hits a column with a ForeignKey it immediately tries 
 to autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be 
 saved.
 
 In fact, I don't ever want to save 'x', and I especially don't want to 
 INSERT it. It would be a duplicate of 'myoldobject'.
 
 Is there a way to copy an orm object and tell it don't ever save this I 
 just want to keep it around to look at?
 
 Alternatively I can just copy all the attributes to a dict(), but that's a 
 bit messy.
 
 dont put it in the Session.
 
 That makes sense. But how do I not do that? As in your example code above I'm 
 not adding it to a session, at least not intentionally.

don't call Session.add().  the code above doesn't add to a session unless 
you're using some special extensions like Session.mapper.



 
 Thanks,
 Michael
 
 -- 
 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 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 sqlalch...@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] orm object, before after

2010-10-16 Thread Michael Hipp

On 10/16/2010 1:55 PM, Michael Bayer wrote:


On Oct 16, 2010, at 2:03 PM, Michael Hipp wrote:


On 10/16/2010 12:52 PM, Michael Bayer wrote:


On Oct 16, 2010, at 1:02 PM, Michael Hipp wrote:


On 8/24/2010 9:47 PM, Michael Bayer wrote:

Michael Hipp wrote:

How do I make a copy of an orm object such that modifications to the
copy do not affect the original?


The mapped object has a member _sa_instance_state that you basically
don't want to transfer to your new object.You want it to have its own
_sa_instance_state and this comes from calling the plain constructor,
which the copy module, if that's what you're using, does not use.   You
also want to set attributes normally, not populating __dict__ directly.
So just basically don't use the copy module.

 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))


Resurrecting an old thread ...

I'm just now getting around to try this but I'm finding out it doesn't really 
work like I'd hoped.

As soon as 'getattr' hits a column with a ForeignKey it immediately tries to 
autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be saved.

In fact, I don't ever want to save 'x', and I especially don't want to INSERT 
it. It would be a duplicate of 'myoldobject'.

Is there a way to copy an orm object and tell it don't ever save this I just want 
to keep it around to look at?

Alternatively I can just copy all the attributes to a dict(), but that's a bit 
messy.


dont put it in the Session.


That makes sense. But how do I not do that? As in your example code above I'm 
not adding it to a session, at least not intentionally.


don't call Session.add().  the code above doesn't add to a session unless 
you're using some special extensions like Session.mapper.


As shown exactly in the code above, I do *not* call Session.add().

It appears to be doing the add when it hits the ForeignKey field. Then it not 
only does an add it also attempts an INSERT (which thankfully fails since the 
object is not ready to be saved).


Michael


--
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] orm object, before after

2010-10-16 Thread Michael Bayer

On Oct 16, 2010, at 4:02 PM, Michael Hipp wrote:

 On 10/16/2010 1:55 PM, Michael Bayer wrote:
 
 On Oct 16, 2010, at 2:03 PM, Michael Hipp wrote:
 
 On 10/16/2010 12:52 PM, Michael Bayer wrote:
 
 On Oct 16, 2010, at 1:02 PM, Michael Hipp wrote:
 
 On 8/24/2010 9:47 PM, Michael Bayer wrote:
 Michael Hipp wrote:
 How do I make a copy of an orm object such that modifications to the
 copy do not affect the original?
 
 The mapped object has a member _sa_instance_state that you basically
 don't want to transfer to your new object.You want it to have its own
 _sa_instance_state and this comes from calling the plain constructor,
 which the copy module, if that's what you're using, does not use.   You
 also want to set attributes normally, not populating __dict__ directly.
 So just basically don't use the copy module.
 
 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))
 
 Resurrecting an old thread ...
 
 I'm just now getting around to try this but I'm finding out it doesn't 
 really work like I'd hoped.
 
 As soon as 'getattr' hits a column with a ForeignKey it immediately tries 
 to autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be 
 saved.
 
 In fact, I don't ever want to save 'x', and I especially don't want to 
 INSERT it. It would be a duplicate of 'myoldobject'.
 
 Is there a way to copy an orm object and tell it don't ever save this I 
 just want to keep it around to look at?
 
 Alternatively I can just copy all the attributes to a dict(), but that's 
 a bit messy.
 
 dont put it in the Session.
 
 That makes sense. But how do I not do that? As in your example code above 
 I'm not adding it to a session, at least not intentionally.
 
 don't call Session.add().  the code above doesn't add to a session unless 
 you're using some special extensions like Session.mapper.
 
 As shown exactly in the code above, I do *not* call Session.add().
 
 It appears to be doing the add when it hits the ForeignKey field. Then it not 
 only does an add it also attempts an INSERT (which thankfully fails since the 
 object is not ready to be saved).

you mean a relationship().  Yeah, if you do this:

x = MyObject()
foo_bar = Session.query(FooBar).first()
x.some_related = foo_bar

where some_related has a backref to collection_of_myobject, the cascade 
will put it in to that session which FooBar belongs.   This behavior is 
configurable via the cascade argument on relationship(), and even more so 
using the new cascade_backrefs flag available in 0.6.5.But also you need 
to decide what you are really trying to do here, if you even want FooBar 
populated on your non-persisted object.









-- 
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] orm object, before after

2010-08-25 Thread Michael Hipp

On 8/24/2010 9:47 PM, Michael Bayer wrote:

Michael Hipp wrote:

How do I make a copy of an orm object such that modifications to the
copy do not affect the original?


 x = MyObject()
 for a in dir(myoldobject):
 if not a.startswith('_'):
 setattr(x, a, getattr(myoldobject, a))


Thank you. That's perfect - and worthy of being captured somewhere it 
can be reused.


I apologize for wasting your time with a poorly worded question.

Thanks,
Michael

--
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] orm object, before after

2010-08-24 Thread Michael Hipp
I'm holding an orm object that will have changes made to it. Once done it will 
be passed to the business logic layer that will have to make decisions from the 
before and after state of the object...


What's the best way to get an object, save its state ('before'), modify it 
('after) without any chance of the modifications creeping into the before? 
Assume both copies are from the same session.


Thanks,
Michael

--
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] orm object, before after

2010-08-24 Thread Michael Bayer

On Aug 24, 2010, at 2:08 PM, Michael Hipp wrote:

 I'm holding an orm object that will have changes made to it. Once done it 
 will be passed to the business logic layer that will have to make decisions 
 from the before and after state of the object...
 
 What's the best way to get an object, save its state ('before'), modify it 
 ('after) without any chance of the modifications creeping into the before? 
 Assume both copies are from the same session.

You'd probably call session.flush() (or commit(), depending on how you are 
scoping your transaction around this operation) before you do anything to it.   
Then, if you'd like the subsequent modifications to not go to the database at 
all until some later point, you'd proceed with your subsequent operations with 
autoflush turned off - recipes for that are at 
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DisableAutoflush .



 
 Thanks,
 Michael
 
 -- 
 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 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 sqlalch...@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] orm object, before after

2010-08-24 Thread Michael Hipp

On 8/24/2010 1:51 PM, Michael Bayer wrote:


On Aug 24, 2010, at 2:08 PM, Michael Hipp wrote:


I'm holding an orm object that will have changes made to it. Once done it will 
be passed to the business logic layer that will have to make decisions from the 
before and after state of the object...

What's the best way to get an object, save its state ('before'), modify it 
('after) without any chance of the modifications creeping into the before? 
Assume both copies are from the same session.


You'd probably call session.flush() (or commit(), depending on how you are 
scoping your transaction around this operation) before you do anything to it.   
Then, if you'd like the subsequent modifications to not go to the database at 
all until some later point, you'd proceed with your subsequent operations with 
autoflush turned off - recipes for that are at 
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DisableAutoflush .


Thank you. But I didn't understand any of that ... at least as it 
relates to my question.


How do I make a copy of an orm object such that modifications to the 
copy do not affect the original?


(Obviously I could detach one of them, but then it becomes useless as 
none of the attributes can be accessed.)


Could you perhaps repeat the answer in baby-talk language?

Thanks,
Michael

--
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] orm object, before after

2010-08-24 Thread Michael Bayer
Michael Hipp wrote:
 On 8/24/2010 1:51 PM, Michael Bayer wrote:

 On Aug 24, 2010, at 2:08 PM, Michael Hipp wrote:

 I'm holding an orm object that will have changes made to it. Once done
 it will be passed to the business logic layer that will have to make
 decisions from the before and after state of the object...

 What's the best way to get an object, save its state ('before'), modify
 it ('after) without any chance of the modifications creeping into the
 before? Assume both copies are from the same session.

 You'd probably call session.flush() (or commit(), depending on how you
 are scoping your transaction around this operation) before you do
 anything to it.   Then, if you'd like the subsequent modifications to
 not go to the database at all until some later point, you'd proceed with
 your subsequent operations with autoflush turned off - recipes for that
 are at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DisableAutoflush
 .

 Thank you. But I didn't understand any of that ... at least as it
 relates to my question.

 How do I make a copy of an orm object such that modifications to the
 copy do not affect the original?

I'm sorry for misunderstanding, but if you read your original question
you'll note that you used the word copies exactly once and not in any
way that made much sense - the main noun was an orm object, singular,
and the next paragraph referred again to an object and it.

The mapped object has a member _sa_instance_state that you basically
don't want to transfer to your new object.You want it to have its own
_sa_instance_state and this comes from calling the plain constructor,
which the copy module, if that's what you're using, does not use.   You
also want to set attributes normally, not populating __dict__ directly.  
So just basically don't use the copy module.

Just construct a new object.

x = MyObject(foo=myoldobject.foo, bar=myoldobject.bar).

or

x = MyObject()
for a in dir(myoldobject):
if not a.startswith('_'):
setattr(x, a, getattr(myoldobject, a))

 Could you perhaps repeat the answer in baby-talk language?

Babies don't know how to program computers in the first place, so I'd aim
higher than that.


-- 
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 options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.