On 20/06/2019 16:00, Mike Bayer wrote:
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]),
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.
Yikes, that escalated quickly.
It probably makes sense just to lose the polymorphic-ness in this case,
I'm not getting enough from it to make the above sensible...
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/4e56afd0-14ca-e767-7e54-39d0813b8f1a%40withers.org.
For more options, visit https://groups.google.com/d/optout.