On Thu, Jun 20, 2019, at 3:14 AM, Chris Withers wrote:
> Hi All,
> 
> I'm getting this warning:
> 
> SAWarning: Flushing object <Cancelled at 0x10b1b9d30> with incompatible 
> polymorphic identity <Types.done: 'DONE'>; the object may not refresh 
> and/or load correctly (this warning may be suppressed after 10 occurrences)
>  (state_str(state), dict_[polymorphic_key]),
> 
> I know why: I'm changing the polymorphic type of a row, but this is 
> intentional (it's handling a PUT to the entity the row represents) and I 
> don't believe I'll hit refresh or load problems since once the 
> transaction is committed, I'm done with the session.
> 
> So, two questions:
> 
> How can I indicate in my code that this is intentional and no warning 
> should be omitted?

you would need to change its class, or replace it with a new object of the 
correct class. in both cases you probably need to pull the object out of the 
Session and put it in again. if you are making a new object, you would use 
make_transient_to_detached() to give the object a persistent primary key 
identity then use session.add() to put it back in again: 

https://docs.sqlalchemy.org/en/13/orm/session_api.html?highlight=make_transient#sqlalchemy.orm.session.make_transient_to_detached

if you are doing "obj.__class__ = newclass", you probably need to do the same 
thing plus more because the identity key needs to have the new class in it, so 
you could make_transient() to erase the old identity key and then 
make_transient_to_detached() to give it back a new identity key, then 
session.add() it.

in both of *those* cases, the history of the object is reset to "clean", so 
anything you actually want to persist on the object needs to be applied after 
you've recreated it as a new class with a new identity key.

still another way, that would not reset the history, you can also manipulate 
state.key directly to have the correct identity key and then replace the 
object, e.g.

session.expunge(obj)
obj.__class__ = newclass
obj.polymorphic_identity = 'new_identity'
inspect(obj).key = sqlalchemy.orm.identity_key(instance=obj)
session.add(obj)

I'd probably want to make a new object and not change __class__, because 
changing __class__ in the general case indicates you're going to have an object 
where its __dict__ can be in an invalid state vs. what the class expects.



> 
> Why does the following warning filter not work?
> 
> warnings.filterwarnings("ignore", category=SADeprecationWarning)
> warnings.filterwarnings("ignore", category=SAWarning)
> 
> The SADeprecationWarning filter *does* work (and is somewhat legit as 
> they're coming from a third party library that's awaiting a fix), but 
> the SAWarning doesn't catch the above.

that is news to me, if that can be demonstrated that should be posted as a new 
bug. suspect the _hash_limit_string() aspect that is doing the "suppressed 
after 10 occurrences" part.


> 
> cheers,
> 
> Chris
> 
> -- 
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/04e4866e-6636-626c-c4f3-16e31849aad2%40withers.org.
> 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/4295d9b5-f537-4ca4-8e75-0472a1a62552%40www.fastmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to