Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2018-01-04 Thread Mike Bayer
On Thu, Jan 4, 2018 at 11:08 AM,   wrote:
> Thank you Mike!
>
> So suppose I have an a Manager object manager and I wanted to promote that
> to Engineer:
>
> # Modify the persisted data of the manager object in the db directly.
> session = object_session(manager)
> session.execute(manager.__table__.update() \
>  .where(manager.__table__.c.id ==
> manager.id) \
>  .values({'type': 'engineer'}))
> # session.flush() ?
>
> # Expunge the object.
> session.expunge(manager)
>
> # Subsequent reads would then return Engineer objects…
>
> I suspect that modifying the object directly would not work with expunge().
> However, if I wouldn't expunge() and all I do is modify the object then
> perhaps that's ok?

you'd have to reassign the class, e.g. manager.__class__ = Engineer.
 You can try all that but I can't guarantee everything will always
work, not really sure.


>
> # Modify the object's type directly, then manager's class would not be
> accurate anymore.
> manager.type = "engineer"
>
> Also, if for some reason the session were to rollback() then the above
> execute() would not be persisted and manager remain a manager. Correct?

in the database, sure.  if you modified the class of your object, that
would still be there.   it would get pretty broken quick.



>
> Thanks!
> Jens
>
>
> On Thursday, August 10, 2017 at 5:25:37 PM UTC+2, Mike Bayer wrote:
>>
>> > Talking about the example in the documentation, do I understand you
>> > correctly that changing from Manager to Engineer is as simple as
>> > updating
>> > the type field?
>>
>> yes.   also locally you want to clear out those objects from your
>> Session and reload (cleanest would be to expunge() totally).
>>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2018-01-04 Thread jens . troeger
Thank you Mike!

So suppose I have an a Manager object *manager* and I wanted to promote 
that to Engineer:

# Modify the persisted data of the manager object in the db directly.
session = object_session(manager)
session.execute(manager.__table__.update() \
 .where(manager.__table__.c.id == manager.id
) \
 .values({'type': 'engineer'}))
# session.flush() ?

# Expunge the object.
session.expunge(manager)

# Subsequent reads would then return Engineer objects…

I suspect that modifying the object directly would not work with expunge(). 
However, if I wouldn't expunge() and all I do is modify the object then 
perhaps that's ok?

# Modify the object's type directly, then manager's class would not be 
accurate anymore.
manager.type = "engineer"

Also, if for some reason the session were to rollback() then the above 
execute() would not be persisted and manager remain a manager. Correct?

Thanks!
Jens


On Thursday, August 10, 2017 at 5:25:37 PM UTC+2, Mike Bayer wrote:
>
> > Talking about the example in the documentation, do I understand you 
> > correctly that changing from Manager to Engineer is as simple as 
> updating 
> > the type field? 
>
> yes.   also locally you want to clear out those objects from your 
> Session and reload (cleanest would be to expunge() totally). 
>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2017-08-10 Thread Mike Bayer
On Wed, Aug 9, 2017 at 8:04 PM,   wrote:
> Thanks, Mike!
>
> My implementation uses the single table inheritance model and I would like
> to (i.e. need to) keep the id the same.
>
> Talking about the example in the documentation, do I understand you
> correctly that changing from Manager to Engineer is as simple as updating
> the type field?

yes.   also locally you want to clear out those objects from your
Session and reload (cleanest would be to expunge() totally).

>
> Cheers,
> Jens
>
>
> On Thursday, August 10, 2017 at 9:18:50 AM UTC+10, Mike Bayer wrote:
>>
>> On Wed, Aug 9, 2017 at 6:53 PM,   wrote:
>> > Hi,
>> >
>> > I came upon this thread because I've got almost the exact same question.
>> > Several years on, though, how would I go about promoting polymorphic
>> > objects
>> > across siblings today?
>>
>> UPDATE the discriminator name, perform INSERT / DELETE of joined
>> inheritance tables as needed
>>
>> > Thank you!
>> > Jens
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2017-08-09 Thread jens . troeger
Thanks, Mike!

My implementation uses the single table inheritance 

 
model and I would like to (i.e. *need to*) keep the id the same.

Talking about the example in the documentation, do I understand you 
correctly that changing from Manager to Engineer is as simple as updating 
the type field?

Cheers,
Jens


On Thursday, August 10, 2017 at 9:18:50 AM UTC+10, Mike Bayer wrote:
>
> On Wed, Aug 9, 2017 at 6:53 PM,  > 
> wrote: 
> > Hi, 
> > 
> > I came upon this thread because I've got almost the exact same question. 
> > Several years on, though, how would I go about promoting polymorphic 
> objects 
> > across siblings today? 
>
> UPDATE the discriminator name, perform INSERT / DELETE of joined 
> inheritance tables as needed 
>
> > Thank you! 
> > Jens 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2017-08-09 Thread Mike Bayer
On Wed, Aug 9, 2017 at 6:53 PM,   wrote:
> Hi,
>
> I came upon this thread because I've got almost the exact same question.
> Several years on, though, how would I go about promoting polymorphic objects
> across siblings today?

UPDATE the discriminator name, perform INSERT / DELETE of joined
inheritance tables as needed


>
> Thank you!
> Jens
>
>
> On Tuesday, February 9, 2010 at 4:09:23 AM UTC+10, Michael Bayer wrote:
>>
>> Pavel Andreev wrote:
>> > Another quick question: if all I need to do is change the
>> > discriminator column, that is, if I'm sure no other tables are
>> > affected, is there a way to do this without manual table updates?
>> > Simply assigning the attribute doesn't seem to trigger table update.
>>
>> the discriminator is hardwired to the class.  so as long as thats what it
>> sees its going to use that discriminator value.  you'd need to change this
>> over in the manual thing you're doing.
>>
>> >
>> > Pavel
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sqlalchemy" group.
>> > To post to this group, send email to sqlal...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > sqlalchemy+...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/sqlalchemy?hl=en.
>> >
>> >
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2017-08-09 Thread jens . troeger
Hi,

I came upon this thread because I've got almost the exact same question. 
Several years on, though, how would I go about promoting polymorphic 
objects across siblings today?

Thank you!
Jens


On Tuesday, February 9, 2010 at 4:09:23 AM UTC+10, Michael Bayer wrote:
>
> Pavel Andreev wrote:
> > Another quick question: if all I need to do is change the
> > discriminator column, that is, if I'm sure no other tables are
> > affected, is there a way to do this without manual table updates?
> > Simply assigning the attribute doesn't seem to trigger table update.
>
> the discriminator is hardwired to the class.  so as long as thats what it
> sees its going to use that discriminator value.  you'd need to change this
> over in the manual thing you're doing.
>
> >
> > Pavel
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "sqlalchemy" group.
> > To post to this group, send email to sqlal...@googlegroups.com 
> .
> > To unsubscribe from this group, send email to
> > sqlalchemy+...@googlegroups.com .
> > For more options, visit this group at
> > http://groups.google.com/group/sqlalchemy?hl=en.
> >
> >
>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: changing polymorphic identity on the fly

2010-02-08 Thread Michael Bayer
Pavel Andreev wrote:
> Another quick question: if all I need to do is change the
> discriminator column, that is, if I'm sure no other tables are
> affected, is there a way to do this without manual table updates?
> Simply assigning the attribute doesn't seem to trigger table update.

the discriminator is hardwired to the class.  so as long as thats what it
sees its going to use that discriminator value.  you'd need to change this
over in the manual thing you're doing.



>
> Pavel
>
> --
> 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.