On 6/29/15 8:47 AM, Burak Arslan wrote:
hello,

here's a test case:

https://gist.github.com/plq/5ed0c135222ea76d77fc (also see below)

is it possible to preserve the polymorphic_identity values of subclasses that contain changes to non-sqla parts of a class?

I understand that the reason for this is D2.__mapper__.class_ != D2. Is there a way to clone the class mapper and change just the mapped class?

you can do this:

class D2(D):
    foo = 'bar'
    __mapper_args__ = {
        'polymorphic_identity': 2,
    }


though you'll get a warning.

SQLAlchemy expects that you will build up the functionality of your class *underneath* the mapping, that is, you can use mixins and __abstract__ to apply behaviors to your class, but the descendant-most class is the thing you're persisting. if you say session.add(D()) and session.add(D2()), the ORM will give you indeterminate results; if an identity is already present and it's a D2, you'll get a D2 back from a result set, otherwise you'll get a D. The design of mappings discourages designs that would produce this issue.






best,
burak


ps: the test case:

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class C(Base):
__tablename__ = 'C'
id = Column(Integer, primary_key=True)
t = Column(Integer)
__mapper_args__ = {
'polymorphic_on': t,
'polymorphic_identity': 1,
}
assert C().t == 1
class D(C):
__mapper_args__ = {
'polymorphic_identity': 2,
}
assert D().t == 2
class D2(D):
foo = 'bar'
assert D2().t == 2

--
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 [email protected] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to